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
🌐
PHP
php.net › manual › en › function.array-values.php
PHP: array_values - Manual
Doing this will cause PHP exceeds the momory limits: <?php $bigArray = array_values( $bigArray ); ?> It's because array_values() does not re-index $bigArray directly, it just re-index it into another array, and assign to itself later. up · down · 20 abimaelrc ¶ · 15 years ago · This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x <?php /** * Get all values from specific key in a multidimensional array * * @param $key string * @param $arr array * @return null|string|array */ function array_value_recursive($key, array $arr){ $val = array(); array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v); }); return count($val) > 1 ?
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.

🌐
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', ), ); I want to get the name value from the ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › foreach-loop-through-multidimensional-array-in-php.php
Foreach Loop through Multidimensional Array in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can simply use the foreach loop in combination with the for loop to access ...
🌐
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:
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 51137644 › get-a-value-from-multidimensional-array-in-php
Get a value from multidimensional array in php? - Stack Overflow
I got this array from API call, I can get the name of the event using the cell number, using this PHP code: $eventName = $str['result'][0]['tokens'][0]['value']; But the number of cells may change...
🌐
SitePoint
sitepoint.com › php
Get Values from nested multidimensional Array - PHP - SitePoint Forums | Web Development & Design Community
October 8, 2014 - Hi I am trying to get “fulltext” values from this json file on this Link I am getting only top of the array how can I get all values from all arrays even the nested ones $json_output = json_decode($json, true); var_dump($json_output); ...
🌐
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.
🌐
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...
🌐
SitePoint
sitepoint.com › php
Get all values from a multidimensional array into a new array - PHP - SitePoint Forums | Web Development & Design Community
May 18, 2016 - <?php //Now I'd like to do a for loop in which I get the value for each array foreach ($VideoCategorys as $Category) { query_posts('category_name=$Category'); if ( have_posts() ) : while ( have_posts() ) : the_post(); echo '<h2>'; the_title(); echo '<h2>'; endwhile; endif; wp_reset_query(); } ?> <?php wp_reset_query(); ?> However, I just realized that $VideoCategorys = get_field(‘video_headers_list’); is giving me a multidimensional array.
🌐
Stack Overflow
stackoverflow.com › questions › 38692702 › get-value-from-array-multidimensional
php - get value from array Multidimensional - Stack Overflow
Array [0] => Array ( [0] => Name [1] => Gender [2] => Age) [1]=> Array ( [0] => John [1] => M [2] => 22 ) [2]=> Array ( [0] => Jack [1] => M [2] => 20 ) This is so simple but im sorry I don't know how :'( I want to get only the Age value from each person (not include header) for parameters in my function countAge($age).
🌐
Developer Drive
developerdrive.com › home › php arrays: array functions and multidimensional arrays
PHP Arrays: Array Functions and Multidimensional Arrays - Developer Drive
January 10, 2022 - The easiest way to loop through a multidimensional array is to nest two foreach loops; the outer loop goes through each outer array element, and the inner loop goes through each inner array element within the selected outer element. <? foreach ($arrBooks as $obj_key =>$book) { echo "$obj_key ...
🌐
MeshWorld
meshworld.in › home › blog › php › how to get values from multi-dimensional arrays with array_column()
How to Get Values from Multi-dimensional Arrays with array_column()
November 15, 2018 - array_column() extracts the values of a single column from a multi-dimensional array or an array of objects. It replaces the foreach loop that builds a list of one field, and it does more than that once you use its third argument. array_column(array ...
Top answer
1 of 4
2

I have created a function that you can more or less give it any product and it will return the key and value from the array

<?php

class Products
{

    public function products_list()
    {

        $customerType = 'national';
        $productType = 'phones';
        $phoneType = 'Apple';
        $productsArr[] = array(
            'customerType' => 'national', 'productType' => array(
                'phones' => array(
                    'Apple' => 'iPhone',
                    'Sony' => 'Xperia',
                    'Samsung' => 'Galaxy'
                ),
                'cases' => array(
                    'leather' => 'black',
                    'plastic' => 'red',
                ),
            ),
            'international' => array(
                'phones' => array(
                    'BlackBerry' => 'One',
                    'Google' => 'Pixel',
                    'Samsung' => 'Note'
                ),
                'cases' => array(
                    'leather' => 'blue',
                    'plastic' => 'green'
                ),
            )
        );

        echo $this->get_value($phoneType, $productsArr) .'<br>';
        echo $this->get_value($customerType, $productsArr) .'<br>';
        echo $this->get_value($productType, $productsArr) .'<br>';
    }

    function get_value($product, array $products)
    {
        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($products), RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($iterator as $key => $value) {
            if (is_string($value) && ($key == $product)) {
                return 'key ->' . $key .' value ->'.$value;
            }
        }
        return "";
    }
}

$phone_products = new Products();


$phone_products->products_list();

To use it within the class just call

$this->get_value($phoneType, $productsArr);

from without the class call

$phone_products = new Products();



echo ($phone_products->get_value($phoneType, $productsArr));
//output: key ->Apple value ->iPhone

NB: $phoneType, $productsArr will either be defined the methods they are being used in or passed from other methods or define global variables within the class.

2 of 4
1

If you want single entry:

 echo $productsArr[0]['productType']['phones']['Apple']."<br />";

If there are multiple data, you can use foreach:

    foreach ($productsArr as $prod){
        echo $prod['productType']['phones']['Apple']."<br />";
    }