For people who searched for php multidimensional array get values and actually want to solve problem comes from getting one column value from a 2 dimensinal array (like me!), here's a much elegant way than using foreach, which is array_column

For example, if I only want to get hotel_name from the below array, and form to another array:

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
    ]
];

I can do this using array_column:

$hotel_name = array_column($hotels, 'hotel_name');

print_r($hotel_name); // Which will give me ['Hotel A', 'Hotel B']

For the actual answer for this question, it can also be beautified by array_column and call_user_func_array('array_merge', $twoDimensionalArray);

Let's make the data in PHP:

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 1,
                    'price' => 200
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 2,
                    'price' => 150
                ]
            ],
        ]
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 3,
                    'price' => 900
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 4,
                    'price' => 300
                ]
            ],
        ]
    ]
];

And here's the calculation:

$rooms = array_column($hotels, 'rooms');
$rooms = call_user_func_array('array_merge', $rooms);
$boards = array_column($rooms, 'boards');

foreach($boards as $board){
    $board_id = $board['board_id'];
    $price = $board['price'];
    echo "Board ID is: ".$board_id." and price is: ".$price . "<br/>";
}

Which will give you the following result:

Board ID is: 1 and price is: 200
Board ID is: 2 and price is: 150
Board ID is: 3 and price is: 900
Board ID is: 4 and price is: 300
Answer from Ng Sek Long on Stack Overflow
Top answer
1 of 2
41

For people who searched for php multidimensional array get values and actually want to solve problem comes from getting one column value from a 2 dimensinal array (like me!), here's a much elegant way than using foreach, which is array_column

For example, if I only want to get hotel_name from the below array, and form to another array:

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
    ]
];

I can do this using array_column:

$hotel_name = array_column($hotels, 'hotel_name');

print_r($hotel_name); // Which will give me ['Hotel A', 'Hotel B']

For the actual answer for this question, it can also be beautified by array_column and call_user_func_array('array_merge', $twoDimensionalArray);

Let's make the data in PHP:

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 1,
                    'price' => 200
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 2,
                    'price' => 150
                ]
            ],
        ]
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 3,
                    'price' => 900
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 4,
                    'price' => 300
                ]
            ],
        ]
    ]
];

And here's the calculation:

$rooms = array_column($hotels, 'rooms');
$rooms = call_user_func_array('array_merge', $rooms);
$boards = array_column($rooms, 'boards');

foreach($boards as $board){
    $board_id = $board['board_id'];
    $price = $board['price'];
    echo "Board ID is: ".$board_id." and price is: ".$price . "<br/>";
}

Which will give you the following result:

Board ID is: 1 and price is: 200
Board ID is: 2 and price is: 150
Board ID is: 3 and price is: 900
Board ID is: 4 and price is: 300
2 of 2
40

This is the way to iterate on this array:

foreach($hotels as $row) {
       foreach($row['rooms'] as $k) {
             echo $k['boards']['board_id'];
             echo $k['boards']['price'];
       }
}

You want to iterate on the hotels and the rooms (the ones with numeric indexes), because those seem to be the "collections" in this case. The other arrays only hold and group properties.

🌐
PHP
php.net › manual › en › function.array-column.php
PHP: array_column - Manual
If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.
🌐
W3Schools
w3schools.com › php › php_arrays_multidimensional.asp
PHP Multidimensional Arrays
To loop through a multidimensional array, use a for loop or a foreach loop. Here, we use a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):
🌐
SitePoint
sitepoint.com › php
Multidimensional arrays - Getting a value without loops - PHP - SitePoint Forums | Web Development & Design Community
October 5, 2018 - Hi, I have an array like the following: $array = array( 0 => array( 'user' => 1, 'name' => 'Name 1', ), 1 => array( 'user' => 2, 'name' => 'Name 2', ), 2 => array( 'user' => 3, 'name' => 'Name 3', ), …
🌐
Uptimia
uptimia.com › home › questions › how to search a php multidimensional array by value?
How To Search A PHP Multidimensional Array By Value?
November 5, 2024 - Two useful functions are array_column() and array_search(). array_column() takes a column from a multidimensional array. It needs three inputs: the array, the column key to extract, and an optional index key.
🌐
Javatpoint
javatpoint.com › php-multidimensional-array-search-by-value
PHP Multidimensional Array Search By Value - javatpoint
PHP The is a built-in function of PHP that dumps the information about the variables. This information includes the data type and value of the variable. In case of string, it also includes the size of the string passed inside the function. The array and object...
🌐
ItSolutionstuff
itsolutionstuff.com › post › php-multidimensional-array-search-by-value-exampleexample.html
PHP Multidimensional Array Search By Value Example - ItSolutionstuff.com
May 14, 2024 - I’m going to show you about php multidimensional array search key by value. We will look at example of how to search value in multidimensional array in php. Let's get started with how to search by key= value in a multidimensional array in php. If you need to get find value from multidimensional array in php.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-search-by-keyvalue-in-a-multidimensional-array-in-php
How to search by key=&gt;value in a multidimensional array in PHP ? - GeeksforGeeks
July 15, 2024 - ... <?php // PHP program to carry out multidimensional // array search by key=>value // Function to iteratively search for a // given key=>value function search($array, $key, $value) { // RecursiveArrayIterator to traverse an // unknown amount ...
Find elsewhere
🌐
Quora
quora.com › How-do-I-fetch-the-key-value-from-a-multidimensional-array
How to fetch the key value from a multidimensional array - Quora
Below are concise, language-agnostic ... (PHP, JavaScript, Python) plus techniques for searching by key or by value. ... Multidimensional array = array whose elements may themselves be arrays (nested dictionaries/maps). ... Given a path (sequence of keys/indices), get the value ...
🌐
GitHub
gist.github.com › 53a9b4e85cfc460eb6c22346ceb68f01
Get all values from specific key in a multidimensional array, similar to array_columns · GitHub
Get all values from specific key in a multidimensional array, similar to array_columns - array-value-recursive.php
🌐
Quora
quora.com › Learning-PHP-Is-there-a-way-to-get-the-value-of-multi-dimensional-array-by-specifying-the-key-with-a-variable
Learning PHP: Is there a way to get the value of multi-dimensional array by specifying the key with a variable? - Quora
Answer (1 of 14): [code]$key = '["a"]["b"]'; echo $array{$key} [/code]will not and should not work. This will work, but I’m not clear on exactly what you’re trying to do, so this may not be the best answer either: [code]$one = $_REQUEST["user_input_value_for_a"]; $two = $_REQUEST["user...
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-multidimensional-array-search-by-value
PHP multidimensional array search by value - GeeksforGeeks
July 11, 2025 - $ --> school3 --> data --> name 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.
🌐
FlatCoding
flatcoding.com › home › php multidimensional arrays: data manipulation
PHP Multidimensional Arrays: Data Manipulation - FlatCoding
April 15, 2025 - In this example, we used two nested loops to access each key-value pair in the multidimensional array. The outer loop iterates through each product array, while the inner loop iterates through each key-value pair within the product array. We then print out each key-value pair and add a separator between each product. Another method to access the array is by directly referencing the values through the index name or number. Let’s see an example. <?php // Access the second product in the $products array $product = $products[1]; // Access the price of the second product $price = $product["price"]; // Print out the price of the second product echo "The price of Product 2 is $" .
🌐
GeeksforGeeks
geeksforgeeks.org › php › multidimensional-associative-array-in-php
Multidimensional Associative Array in PHP - GeeksforGeeks
July 12, 2025 - The last key i.e. description of each parent key has been associated with another array of the set of keys and constant values. Here Python and PHP are parent key for first_release, latest_release, designed_by and description whereas description is parent key for the extension, typing_discipline, and license. Retrieving Values: We can retrieve the value of multidimensional array using the following method:
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-access-the-elements-of-a-multidimensional-array-in-PHP.php
How to Access the Elements of a Multidimensional Array in PHP
The PHP code below creates the array and outputs the elements of the array. So this is how a multidimensional can be accessed that does not have key-value pairs. We use the name of the array $people, followed by the row number, followed by the column number.