I have the following array structure:
Array ( [0] => Array ( [product_option_id] => 236 [option_id] => 14 [name] => Masura S [type] => select [option_value] => Array ( [0] => Array ( [product_option_value_id] => 33 [option_value_id] => 53 [name] => Alb [price] => [price_prefix] => + ) [1] => Array ( [product_option_value_id] => 35 [option_value_id] => 55 [name] => Rosu [price] => [price_prefix] => + ) ) [required] => 0 ) [1] => Array ( [product_option_id] => 237 [option_id] => 15 [name] => Masura M [type] => select [option_value] => Array ( [0] => Array ( [product_option_value_id] => 34 [option_value_id] => 58 [name] => Rosu [price] => [price_prefix] => + ) ) [required] => 0 ) )
I find myself lost in trying to display all the [name] values from this array.
What I am trying to do is to populate a form with dropdown selects based on the first level [name]
(like [name] => Masura S
) and then a second dropdown select with the second level [name]
(like [name] => Alb
).
I would appreciate it if you have any pointers...
Answer: Use the Array Key or Index If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key.
Multidimensional array search using array_search() method: The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path.
Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.
Creating Multidimensional Arrays You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.
Try this:
$name = array_column($array, 'name');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With