
- #Php associative array inside an associative array how to
- #Php associative array inside an associative array code
An associative array can be created by enclosing a comma-separated list of key-value. See Also See also isarray (), explode (), implode (), pregsplit (), and unset ().
#Php associative array inside an associative array how to
To solve this problem, I decided to create it as an associative array in PHP before feeding it to json_encode.After fetching a result from the database, and preparing the array for the JSON enconde, I face a dilemma on how to reference the array 'data' inside the main array. A PHP associative array is an array that uses strings as its keys. This script was expecting a JSON object with strings as property names - not hard to see why it was breaking when I provided it with an indexed JSON object instead.

I want to set the value of an associative array using the array index of the key/value pair. I had a specific situation where a PHP script was creating a JSON object for JavaScript in the front-end. Accessing an associative array by integer index in PHP.


In some cases it may be nessecery to check if you are dealing with an associative array, as a function might depend on it to work properly, but in most cases you would probably just assume that the array is formatted correctly.
#Php associative array inside an associative array code
You may even choose to use objects instead of arrays if you are working in an OOP context, since objects have other benefits, and properties are supported by the autocompletion feature in some code editors. The difference between indexed arrays and the above associative arrays is that associative array is constructed by adding keys explicitly by us without any. PHP Associative arrays are the arrays with named keys, that means named keys assigned to the elements of an array are used to access the elements of the.

Keep in mind that there is not really a difference between associative- and indexed arrays in PHP. Foreach ( $some_array as $key => $value ) Have You Tried: Associative Arrays You can explode an associative array to return to individual, non-associated objects, which you might need to do if the. Associative array inside array in PHP Ask Question Asked 9 years, 4 months ago Modified 7 years, 6 months ago Viewed 234 times 1 After fetching a result from the database, and preparing the array for the JSON enconde, I face a dilemma on how to reference the array 'data' inside the main array.
