Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array - how to get specific values from sub-array

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...

like image 685
RePho Avatar asked Jun 12 '11 07:06

RePho


People also ask

How can we get single value from this multi dimensional PHP array?

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.

How do you search a multidimensional array?

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.

How do you sort multidimensional arrays?

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.

How do you handle multidimensional arrays?

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.


1 Answers

Try this:

$name = array_column($array, 'name');
like image 128
Usman Ahmed Avatar answered Nov 14 '22 22:11

Usman Ahmed