• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 50
  • Last Modified:

Array help

I have this array below
$postDesc = array(
"client_id" => 900,
"date_from" => '2018-04-03',
"date_to" => '2018-04-03',
"property" => 12      
);

It works but I want to add multiple properties to return how is this done? When I add any it just returns the first one.

For example:
$propDesc = array(
"client_id" => 900,
"date_from" => '2018-04-03',
"date_to" => '2018-04-03',
"property_id" => 12, 14, 25      
);
0
ucohockey
Asked:
ucohockey
4 Solutions
 
NorieVBA ExpertCommented:
Try using an array for the multiple property_id values.

$propDesc = array(
"client_id" => 900,
"date_from" => '2018-04-03',
"date_to" => '2018-04-03',
"property_id" => array(12, 14, 25)
);
1
 
ucohockeyAuthor Commented:
Thanks for the help but it jut returned error.
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
If I use Norie's example and use var_dump to dump the $propDesc variable, it looks like the following:
array(4) {
  ["client_id"] => int(900)
  ["date_from"] => string(10) "2018-04-03"
  ["date_to"] => string(10) "2018-04-03"
  ["property_id"] => array(3) {
    [0] => int(12)
    [1] => int(14)
    [2]=> int(25)
  }
}

Open in new window


There is no error.  What do you mean that it returned an error?  How did you try to access the property?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Julian HansenCommented:
Thanks for the help but it jut returned error.
When suggestions from experts do not work or produce an error - it is useful to post the code that you used that generated the error along with the error. Simply saying that there was an error does not allow us to diagnose what the problem is.

The solution as provided is correct - therefore we can only assume that the error is due to
a) An error made in implementation
b) Some other aspect of the application that you have not revealed to us.
1
 
Olaf DoschkeSoftware DeveloperCommented:
The array design pattern is pretty straightforward key => value. And value can't be a list of values, unless you make it a value that is capable to store a list of values, as an array itself is.

But when you make use of that you get back whatever you store, so when you store an array(12,14,25) instead of a single value 12, you can't handle it as a single scalar value in any code reading this back.

If you can't change your code to handle that an array element might be an array itself, you could also change to store a single scalar value, a string '12,14,25':

$propDesc = array(
"client_id" => 900,
"date_from" => '2018-04-03',
"date_to" => '2018-04-03',
"property_id" => '12, 14, 25'
);

Open in new window


Strings always are a possibility, but also, in this case, you can't expect code previously expecting to get a single numeric value to now work on a list of values in a string. When you change the paradigm of something, that can't only mean the change of a single column in a table, an item of an array or such, some code will need to change, too.

What this helps with instead of an array is, you can store this in a table, it stays a "vanilla" associative array not nesting further finer structures, but if you had a table previously storing a single numeric id at that place, that will still need to change to allow storage of a string being a CSV. And a change of code handling this. And queries for property_id=12 will also not continue to work, even not when changing to property_id='12', now.

In the aspect of storing data in tables, this raises the need to alter your database in a way better than merely storing a CSV instead of a single id. What changed likely is a 1:1 relation becomes 1:n or a 1:n relation became n:m and that is a change requiring a little more work than that.

Bye, Olaf.
2
 
NorieVBA ExpertCommented:
Other experts indicated the solution I provided was correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now