๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_multidimensional.asp
PHP Multidimensional Arrays
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.
๐ŸŒ
Matt Doyle
elated.com โ€บ home โ€บ blog โ€บ using multidimensional arrays in php
Using Multidimensional Arrays in PHP
July 23, 2022 - To do this, you need to create nested foreach loops โ€” that is, one loop inside another: The outer loop reads each element in the top-level array. For each of those top-level elements, the inner loop moves through the array contained in the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ multidimensional-arrays-in-php
Multidimensional arrays in PHP - GeeksforGeeks
July 11, 2025 - It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be used to store any type of elements, but the index is always a number. By default, the index starts with zero.
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php tutorial โ€บ php multidimensional array
PHP Multidimensional Array - PHP Tutorial
April 10, 2025 - Array ( [0] => Array ( [0] => Learn PHP programming [1] => 2 ) [1] => Array ( [0] => Work [1] => 8 ) [2] => Array ( [0] => Do exercise [1] => 1 ) )Code language: PHP (php) To iterate a multidimensional array, you use a nested foreach loop like this: <?php $tasks = [ ['Learn PHP programming', ...
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ multidimensional-arrays-and-their-traversal-in-php โ€บ lessons โ€บ multidimensional-arrays-and-their-traversal-in-php
Multidimensional Arrays and Their Traversal in PHP
<?php // Defining and initializing the array $array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; // Updating an element $array[0][1] = 10; foreach ($array as $floor) { foreach ($floor as $unit) { echo $unit . " "; } echo "\n"; } // Output: 1 10 3 4 5 6 7 8 9 ?> ... Great work! Today we explored various operations on multidimensional arrays, from creating them to updating and traversing them.
๐ŸŒ
egghead.io
egghead.io โ€บ lessons โ€บ php-create-a-nested-or-multi-dimensional-array-in-php
Create a nested or multi-dimensional array in PHP | egghead.io
Storing an array within an array is possible with PHP, and are called 'multi-dimensional' arrays.
Published ย  June 29, 2022
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php multidimensional arrays: data manipulation
PHP Multidimensional Arrays: Data Manipulation - FlatCoding
April 15, 2025 - In this example, we created a nested multidimensional array called $employees that contains three arrays, each representing an employee. Each employee array contains four key-value pairs, including a projects key, which contains an array of the projects the employee is working on. To retrieve the project names for each employee, we can employ another nested loop. Letโ€™s see an example: <?php // Loop through each employee in the $employees array foreach ($employees as $employee) { // Print out the employee name and department echo $employee["name"] .
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php-tutorial โ€บ php multidimensional array
PHP Multidimensional Array - Scaler Topics
April 14, 2024 - To create a multidimensional array in PHP, you can assign an array as the value for each element in an existing array. This nesting of arrays allows you to build the desired structure.
๐ŸŒ
Web Reference
webreference.com โ€บ php โ€บ references โ€บ multidimensional-array
PHP Multidimensional Arrays | WebReference
Here's an example of a multidimensional array: $students = [ ['name' => 'John', 'age' => 30, 'gender' => 'male'], ['name' => 'Jane', 'age' => 25, 'gender' => 'female'], ['name' => 'Bob', 'age' => 28, 'gender' => 'male'] ]; To loop through a multidimensional array, you can use nested foreach loops.
Find elsewhere
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ multidimensional array in php
Multidimensional Array in PHP - Scientech Easy
February 5, 2026 - To access elements in a multidimensional array in PHP, you will have to use multiple indices. The first index represents the outermost array, the second index refers to the first nested array, the third index refers to the second nested array, and so on.
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ php-arrays-advanced-techniques-multidimensional-arrays
PHP Arrays: Advanced Techniques and Multidimensional Arrays
April 28, 2023 - This hybrid nature makes them ... establish a foundation with multidimensional arrays. Multidimensional arrays are arrays containing other arrays as elements, creating nested data structures....
๐ŸŒ
Developer Indian
developerindian.com โ€บ articles โ€บ php-multidimensional-arrays
PHP Multidimensional Arrays
May 11, 2026 - A multidimensional array is an array that contains one or more arrays inside it. ... This is a 2-dimensional array (array of arrays). Each inner array contains data for one student.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ php โ€บ php array: associative, multidimensional
PHP Array: Associative, Multidimensional
June 28, 2024 - Another way to define the same array is as follows ยท <?php $film=array( "comedy" => array( 0 => "Pink Panther", 1 => "john English", 2 => "See no evil hear no evil" ), "action" => array ( 0 => "Die Hard", 1 => "Expendables" ), "epic" => array ( 0 => "The Lord of the rings" ), "Romance" => array ( 0 => "Romeo and Juliet" ) ); echo $film["comedy"][0]; ?>
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ mastering-complex-data-structures-in-php โ€บ lessons โ€บ compound-data-structures-in-php-nested-arrays-and-associative-arrays
Compound Data Structures in PHP: Nested Arrays and ...
The retrieval of values from nested associative or indexed arrays follows rules similar to those for their non-nested counterparts. ... <?php // Associative array within an associative array $nested_map = array( "fruit" => array( "apple" => "red", "banana" => "yellow" ), "vegetable" => array( "carrot" => "orange", "spinach" => "green" ) ); // Accessing apple's color from nested associative array echo $nested_map["fruit"]["apple"]; // Output: red ?>
๐ŸŒ
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โ€ ... all arrays even the nested ones $json_output = json_decode($json, true); var_dump($json_output); foreach($json_output['results'] as $item) { echo ' '. $item['fulltext'];...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ php-multidimensional-array
PHP - Multidimensional Array
September 19, 2020 - We can also employ two nested foreach loops to traverse a 2D associative array. Unpack each row of the outer array in row-key and row-value variables and traverse each row elements with the inner foreach loop. <?php $tbl = [ "row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"], "row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"], "row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"] ]; echo ("\n"); foreach ($tbl as $rk=>$rv){ echo "$rk\n"; foreach ($rv as $k=>$v){ echo "$k => $v "; } echo "\n"; } ?>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-check-an-array-is-multidimensional-or-not-in-php
How to check an array is multidimensional or not in PHP ? - GeeksforGeeks
July 11, 2025 - This method involves writing a function that calls itself to traverse the array and check for nested arrays. Example: In this example, the isMultidimensional function iterates through each element of the given array.
๐ŸŒ
Hashnode
justdevthings.hashnode.dev โ€บ multidimensional-array-in-php
What Is A Multidimensional Array In PHP? - /* Just Dev Things */
May 24, 2023 - Let's explore different methods for looping through multidimensional arrays to manipulate and extract information. One common approach is to use nested foreach loops to traverse each level of the multidimensional array.
Top answer
1 of 1
3

OK, I hope you still need this, because I wasted more time than I'd like to admin getting this right :)

Basically, my approach was to first manipulate the string into the format [set][of][keys]=value, and then loop through the string of keys and comparing them with the last set of keys to create the correct key hierarchy. I used eval because it's easier, but you can write a replacement function if you can't stomach seeing that function in your code:

//FIRST WE GET THE STRING INTO EASIER TO WORK WITH CHUNKS
$original_string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no ";
$cleaned_string = str_replace('] @[','][',$original_string);
/* This results in clusters of keys that equal a value:
@[item_1][door][mozart][grass] = yes @[mozart][green] = no @[mozart][human][blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no 

OR (with line breaks for clarity):

@[item_1][door][mozart][grass] = yes 
@[mozart][green] = no 
@[mozart][human][blue][movie]=yes 
@[item_1][beat] = yes 
@[item_1][music] = no */

//break it up into an array:
$elements = explode('@',$cleaned_string);

//create a variable to compare the last string to
$last_keys = "";
//and another that will serve as our final array
$array_of_arrays = array();
//now loop through each [item_1][door][mozart][grass] = yes,[mozart][green] = no, etc
foreach($elements as $element){
    if ($element==""){continue;} //skip the first empty item

    //break the string into [0] = group of keys and [1] the value that terminates the string 
    //so [item_1][door][mozart][grass] = yes BECOMES [item_1][door][mozart][grass], AND yes
    $pieces = explode('=',str_replace(array('[',']'),array("['","']"),trim($element))); 
    //now compare this set of keys to the last set of keys, and if they overlap merge them into a single key string
    $clean_keys = combine_key_strings($pieces[0],$last_keys);
    //set the new key string the value for the next comparison
    $last_keys = $clean_keys;
    //and (ugly, I know) we use an eval to convert "[item_1][door][mozart][grass]='yes'" into a properly keyed array
    eval("\$array_of_arrays".$clean_keys." = '".trim($pieces[1])."';");
}

//now dump the contents
print_r($array_of_arrays);


//THIS FUNCTION COMPA
function combine_key_strings($new,$old){
    //get the key that starts the newer string
    $new_keys = explode('][',$new);
    $first_key = $new_keys[0].']';

    //see if it appears in the last string
    $last_occurance = strrpos ($old,$first_key);
    //if so, merge the two strings to create the full array keystring
    if (is_int($last_occurance)){
        return substr($old,0,$last_occurance).$new;
    }
    return $new;
}

This should spit out your correctly nested array:

Array
(
    [item_1] => Array
        (
            [door] => Array
                (
                    [mozart] => Array
                        (
                            [grass] => yes
                            [green] => no
                            [human] => Array
                                (
                                    [blue] => Array
                                        (
                                            [movie] => yes
                                        )

                                )

                        )

                )

            [beat] => yes
            [music] => no
        )

)

Good night!