php multidimensional array

PHP Multidimensional Array In Depth: Ultimate Guide

A PHP array is a variable that stores more than one piece of related data in a single variable. A multidimensional array is an array with more than one dimension, such as a two-dimensional array or a three-dimensional array. Multi-dimensional arrays are such type of arrays that stores another array at each index instead of a single element. It should be described as an array of arrays.

As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within. Multidimensional arrays are useful for storing data in a structured way, and they can be used to perform complex calculations.

PHP supports multidimensional arrays that are multi-levels deep. However, arrays more than three levels deep are hard to manage for most people and are also rarely used in projects. In this tutorial, you will learn how to define a PHP multidimensional array and manipulate its elements effectively.

Definition of the multidimensional array in PHP

In this section, you are going to create a one, two, and three-dimension array.

1- Indexed array

The indexed array is an array whose key is number and ordered. It means the index starts from zero and increases with step 1. By default, the index starts with zero.

Typically, you have an array with one dimension.

$cars = array("Ferrari","Benz","BMW","Toyota");

var_dump($cars);

Output

array (size=4)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Toyota' (length=6)

This example contains an indexed one-dimension array. As you see, the keys are not defined in the array but if you print them you will see the keys.

To define a two-dimensional array use the below example.

A two-dimensional array is an array of arrays. It is the simplest form of a multidimensional array. It is like a table of rows and columns. It can be made with the aid of a nested array.

Let’s see a simple example of a PHP multidimensional array to store the data in a two-dimensional array

$cars = array(
    array("Ferrari","Italy"),
    array("Benz","Germany"),
    array("BMW","Germany"),
    array("Toyota","Japan")
);

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=2)
      0 => string 'Ferrari' (length=7)
      1 => string 'Italy' (length=5)
  1 => 
    array (size=2)
      0 => string 'Benz' (length=4)
      1 => string 'Germany' (length=7)
  2 => 
    array (size=2)
      0 => string 'BMW' (length=3)
      1 => string 'Germany' (length=7)
  3 => 
    array (size=2)
      0 => string 'Toyota' (length=6)
      1 => string 'Japan' (length=5)

In this example, there are four arrays that store an array of the car brand and country.

It is the form of a multidimensional array. Initialization in a Three-Dimensional array is the same as that of Two-dimensional arrays. A three-dimensional array is an array of arrays of arrays.

Now let’s define a three-dimensional array.

$cars = array(
    array("Ferrari",
        array("Italy","it")
    ),
    array("Benz",
        array("Germany","due")
    ),
    array("BMW",
        array("Germany","due")
    ),
    array("Toyota",
        array("Japan","jap")
    )
);

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=2)
      0 => string 'Ferrari' (length=7)
      1 => 
        array (size=2)
          0 => string 'Italy' (length=5)
          1 => string 'it' (length=2)
  1 => 
    array (size=2)
      0 => string 'Benz' (length=4)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)
  2 => 
    array (size=2)
      0 => string 'BMW' (length=3)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)
  3 => 
    array (size=2)
      0 => string 'Toyota' (length=6)
      1 => 
        array (size=2)
          0 => string 'Japan' (length=5)
          1 => string 'jap' (length=3)

This example contains a multidimensional array that contains 2 items the first one is the car’s name and the second one is an array that contains the country name and code.

All these arrays are indexed, now let’s define the associative array.

2- Associative array

All associative arrays are similar to indexed arrays but instead of linear (indexed), every value can be assigned with a user-defined key.

$cars = array("fer" => "Ferrari","ben" => "Benz","bmw" => "BMW","toy" => "Toyota");

var_dump($cars);

Output

array (size=4)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'toy' => string 'Toyota' (length=6)

This is a one-dimensional associative array.

Now let’s define two-dimensional.

$cars = array(
    "fer" => array("a" => "Ferrari","b" => "Italy"),
    "ben" => array("a" => "Benz","b" => "Germany"),
    "bmw" => array("a" => "BMW","b" => "Germany"),
    "toy" => array("a" => "Toyota","b" => "Japan")
);

Output

array (size=4)
  'fer' => 
    array (size=2)
      'a' => string 'Ferrari' (length=7)
      'b' => string 'Italy' (length=5)
  'ben' => 
    array (size=2)
      'a' => string 'Benz' (length=4)
      'b' => string 'Germany' (length=7)
  'bmw' => 
    array (size=2)
      'a' => string 'BMW' (length=3)
      'b' => string 'Germany' (length=7)
  'toy' => 
    array (size=2)
      'a' => string 'Toyota' (length=6)
      'b' => string 'Japan' (length=5)

This definition is like the indexed array with a user-defined key. If you don’t define the key, it will be indexed (numbered).

The three-dimensional array is like the two-dimensional array with the extra array.

$cars = array(
    "fer" => 
        array(
            "a" => "Ferrari",
            "b" => array("ba" => "Italy","bb" => "it")
        ),
    "ben" => 
        array(
            "a" => "Benz",
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "bmw" => 
        array(
            "a" => "BMW",
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "toy" => 
        array(
            "a" => "Toyota",
            "b" => array("ba" => "Japan","bb" => "jap")
    )
);

var_dump($cars);

Output

array (size=4)
  'fer' => 
    array (size=2)
      'a' => string 'Ferrari' (length=7)
      'b' => 
        array (size=2)
          'ba' => string 'Italy' (length=5)
          'bb' => string 'it' (length=2)
  'ben' => 
    array (size=2)
      'a' => string 'Benz' (length=4)
      'b' => 
        array (size=2)
          'ba' => string 'Germany' (length=7)
          'bb' => string 'due' (length=3)
  'bmw' => 
    array (size=2)
      'a' => string 'BMW' (length=3)
      'b' => 
        array (size=2)
          'ba' => string 'Germany' (length=7)
          'bb' => string 'due' (length=3)
  'toy' => 
    array (size=2)
      'a' => string 'Toyota' (length=6)
      'b' => 
        array (size=2)
          'ba' => string 'Japan' (length=5)
          'bb' => string 'jap' (length=3)

Access to elements of the multidimensional array in PHP

Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.

To access an element in a multidimensional array, you use the square brackets ([])

There are mainly two ways to access multidimensional array elements in PHP.

  • Elements can be accessed using dimensions as array_name[‘first dimension’][‘second dimension’].
  • Elements can be accessed using PHP loops.

1- Indexed array

The indexed array keys are numbered in order. As mentioned, by default, the index starts with zero. By brackets, you can access elements of the array. No matter it is a one-dimensional array or multidimensional.

$cars = array("Ferrari","Benz","BMW","Toyota");

echo $cars[0];

Output

Ferrari

This is a one-dimensional array and you can access its element simply by []. Now let’s access the two-dimensional array elements.

The second way to access elements is the loop.

$cars = array("Ferrari","Benz","BMW","Toyota");

foreach($cars as $key => $car){
    echo "Key=".$key." and Car name=" .$car . "<br />";
}

Output

Key=0 and Car name=Ferrari
Key=1 and Car name=Benz
Key=2 and Car name=BMW
Key=3 and Car name=Toyota

To iterate an array the foreach loop is the best one. It decreases the code and increases the readability of the code.

Now let’s access the two-dimensional array’s elements.

$cars = array(
    array("Ferrari","Italy"),
    array("Benz","Germany"),
    array("BMW","Germany"),
    array("Toyota","Japan")
);

echo $cars[0][1];

Output

Italy

To get access to the elements of the $cars array we must point to the two indices (row and column):

We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):

$cars = array(
    array("Ferrari","Italy"),
    array("Benz","Germany"),
    array("BMW","Germany"),
    array("Toyota","Japan")
);

for($c = 0; $c<count($cars); $c++){
    echo "Key=".$c." and Car details=<br />";
    for($cd=0; $cd<count($cars[$c]); $cd++){
        echo "-- Key=".$cd." and Car detail=".$cars[$c][$cd]."<br />";
    }
}

Output

Key=0 and Car details=
-- Key=0 and Car detail=Ferrari
-- Key=1 and Car detail=Italy
Key=1 and Car details=
-- Key=0 and Car detail=Benz
-- Key=1 and Car detail=Germany
Key=2 and Car details=
-- Key=0 and Car detail=BMW
-- Key=1 and Car detail=Germany
Key=3 and Car details=
-- Key=0 and Car detail=Toyota
-- Key=1 and Car detail=Japan
$cars = array(
    array("Ferrari",
        array("Italy","it")
    ),
    array("Benz",
        array("Germany","due")
    ),
    array("BMW",
        array("Germany","due")
    ),
    array("Toyota",
        array("Japan","jap")
    )
);

echo $cars[0][1][0];

Output

Italy

Iterate with for loop:

$cars = array(
    array(
        array("Ferrari","fer"),
        array("Italy","it")
    ),
    array(
        array("Benz","ben"),
        array("Germany","due")
    ),
    array(
        array("BMW","bmw"),
        array("Germany","due")
    ),
    array(
        array("Toyota","toy"),
        array("Japan","jap")
    )
);

for($c = 0; $c<count($cars); $c++){
    echo "Key=".$c."<br />";
    for($cd=0; $cd<count($cars[$c]); $cd++){
        echo "-- Key=".$cd."<br />";
        for($cdc=0; $cdc < count($cars[$c][$cd]); $cdc++){
            echo "---- Key=".$cdc." | ".$cars[$c][$cd][$cdc]."<br />";
        }
    }
}

Output

Key=0
-- Key=0
---- Key=0 | Ferrari
---- Key=1 | fer
-- Key=1
---- Key=0 | Italy
---- Key=1 | it
Key=1
-- Key=0
---- Key=0 | Benz
---- Key=1 | ben
-- Key=1
---- Key=0 | Germany
---- Key=1 | due
Key=2
-- Key=0
---- Key=0 | BMW
---- Key=1 | bmw
-- Key=1
---- Key=0 | Germany
---- Key=1 | due
Key=3
-- Key=0
---- Key=0 | Toyota
---- Key=1 | toy
-- Key=1
---- Key=0 | Japan
---- Key=1 | jap

2- Associative array

Associative array has user-defined keys so you can’t access the elements by indexes.

$cars = array("fer" => "Ferrari","ben" => "Benz","bmw" => "BMW","toy" => "Toyota");
echo $cars['fer'];

Output

Ferrari

In this example, you have to know the key.

$cars = array("fer" => "Ferrari","ben" => "Benz","bmw" => "BMW","toy" => "Toyota");
foreach($cars as $key => $car){
    echo "Key=".$key." and Car name=" .$car . "<br />";
}

Output

Key=fer and Car name=Ferrari
Key=ben and Car name=Benz
Key=bmw and Car name=BMW
Key=toy and Car name=Toyota

In this example, we used a foreach loop that is the same as indexed arrays. The difference is that in the indexed array, you had a number as a key and here you have a key name like “fer”.

Two-dimensional array:

$cars = array(
    "fer" => array("a" => "Ferrari","b" => "Italy"),
    "ben" => array("a" => "Benz","b" => "Germany"),
    "bmw" => array("a" => "BMW","b" => "Germany"),
    "toy" => array("a" => "Toyota","b" => "Japan")
);
echo $cars["fer"]["a"];

Output

Ferrari

Now let’s iterate with foreach loop

$cars = array(
    "fer" => array("a" => "Ferrari","b" => "Italy"),
    "ben" => array("a" => "Benz","b" => "Germany"),
    "bmw" => array("a" => "BMW","b" => "Germany"),
    "toy" => array("a" => "Toyota","b" => "Japan")
);
foreach($cars as $key => $car){
    echo "Key=".$key." and Car details=<br />";
    if(is_array($car)){
        foreach($car as $d_key => $car_detail){
            echo "-- Key=".$d_key." and Car detail=".$car_detail."<br />";
        }
    }
}

Output

Key=fer and Car details=
-- Key=a and Car detail=Ferrari
-- Key=b and Car detail=Italy
Key=ben and Car details=
-- Key=a and Car detail=Benz
-- Key=b and Car detail=Germany
Key=bmw and Car details=
-- Key=a and Car detail=BMW
-- Key=b and Car detail=Germany
Key=toy and Car details=
-- Key=a and Car detail=Toyota
-- Key=b and Car detail=Japan

You have to use foreach in foreach to iterate a two-dimensional array. The safe code is if you are not sure the value will be an array or not, use the is_array() function.

Three-dimensional:

$cars = array(
    "fer" => 
        array(
            "a" => array("aa" => "Ferrari","ab" => "fer"),
            "b" => array("ba" => "Italy","bb" => "it")
        ),
    "ben" => 
        array(
            "a" => array("aa" => "Benz","ab" => "ben"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "bmw" => 
        array(
            "a" => array("aa" => "BMW","ab" => "bmw"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "toy" => 
        array(
            "a" => array("aa" => "Toyota","ab" => "toy"),
            "b" => array("ba" => "Japan","bb" => "jap")
    )
);

echo $cars['fer']['a']['aa'];

Output

Ferrari

Iterate with foreach loop

$cars = array(
    "fer" => 
        array(
            "a" => array("aa" => "Ferrari","ab" => "fer"),
            "b" => array("ba" => "Italy","bb" => "it")
        ),
    "ben" => 
        array(
            "a" => array("aa" => "Benz","ab" => "ben"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "bmw" => 
        array(
            "a" => array("aa" => "BMW","ab" => "bmw"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "toy" => 
        array(
            "a" => array("aa" => "Toyota","ab" => "toy"),
            "b" => array("ba" => "Japan","bb" => "jap")
    )
);

foreach($cars as $key => $car){
    echo "Key=".$key."<br />";
    if(is_array($car)){
        foreach($car as $d_key => $car_details){
            echo "-- Key=".$d_key."<br />";
            if(is_array($car_details)){
                foreach($car_details as $cd_key => $car_detail){
                    echo "---- Key=".$cd_key." | ".$car_detail."<br />";
                }
            }
        }
    }
}

Output

Key=fer
-- Key=a
---- Key=aa | Ferrari
---- Key=ab | fer
-- Key=b
---- Key=ba | Italy
---- Key=bb | it
Key=ben
-- Key=a
---- Key=aa | Benz
---- Key=ab | ben
-- Key=b
---- Key=ba | Germany
---- Key=bb | due
Key=bmw
-- Key=a
---- Key=aa | BMW
---- Key=ab | bmw
-- Key=b
---- Key=ba | Germany
---- Key=bb | due
Key=toy
-- Key=a
---- Key=aa | Toyota
---- Key=ab | toy
-- Key=b
---- Key=ba | Japan
---- Key=bb | jap

To iterate with foreach for every level, you have to add extra foreach inside the foreach. The problem is, that you have to know the array dimension. What about the arrays you don’t know its dimension or structure? Or what about accessing the element for which you don’t know its key?

To access elements of the multidimensional array without knowing its structure and dimensions you have to use a recursive function. For example:

$cars = array(
    "fer" => 
        array(
            "a" => array("aa" => "Ferrari","ab" => "fer"),
            "b" => array("ba" => "Italy","bb" => "it")
        ),
    "ben" => 
        array(
            "a" => array("aa" => "Benz","ab" => "ben"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "bmw" => 
        array(
            "a" => array("aa" => "BMW","ab" => "bmw"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "toy" => 
        array(
            "a" => array("aa" => "Toyota","ab" => "toy"),
            "b" => array("ba" => "Japan","bb" => "jap")
    )
);

function array_iterator($array){
    if(is_array($array)){
        foreach($array as $key => $item){
            if(is_array($item)){
                echo "<br />key=". $key;
                array_iterator($item);
            }
            else{
                echo "<br />key=". $key . " value=" . $item;
            }
        }
    }
    else{
        echo "<br />" . $array;
    }
}

array_iterator($cars);

Output


key=fer
key=a
key=aa value=Ferrari
key=ab value=fer
key=b
key=ba value=Italy
key=bb value=it
key=ben
key=a
key=aa value=Benz
key=ab value=ben
key=b
key=ba value=Germany
key=bb value=due
key=bmw
key=a
key=aa value=BMW
key=ab value=bmw
key=b
key=ba value=Germany
key=bb value=due
key=toy
key=a
key=aa value=Toyota
key=ab value=toy
key=b
key=ba value=Japan
key=bb value=jap

This function iterates every level and deep of the array and prints the key and value.

Adding elements to a PHP multidimensional array

To insert an element into the array you have to know which level you want to add the element to. For one-dimensional you could do the following.

$cars = array("Ferrari","Benz");

$cars[] = "BMW";
$cars["toy"]="Toyota";

var_dump($cars);

Output

array (size=4)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  'toy' => string 'Toyota' (length=6)

As you see, you can insert an indexed array without specifying the index and associative element with the key.

Inserting an element into the two-dimensional array is like one dimension.

$cars = array(
    array("Ferrari","Italy"),
    array("Benz","Germany"),
    array("BMW","Germany"),
    array("Toyota","Japan")
);

$cars[0][] = "Luxury";
$cars[0]['model'] = "2022";

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=4)
      0 => string 'Ferrari' (length=7)
      1 => string 'Italy' (length=5)
      2 => string 'Luxury' (length=6)
      'model' => string '2022' (length=4)
  1 => 
    array (size=2)
      0 => string 'Benz' (length=4)
      1 => string 'Germany' (length=7)
  2 => 
    array (size=2)
      0 => string 'BMW' (length=3)
      1 => string 'Germany' (length=7)
  3 => 
    array (size=2)
      0 => string 'Toyota' (length=6)
      1 => string 'Japan' (length=5)

Inserting is like the previous example with a little different in that this time you have to specify the index or key of the first level.

Now you can guess how to insert an element into a three-dimensional array in PHP.

$cars = array(
    array(
        array("Ferrari","fer"),
        array("Italy","it")
    ),
    array(
        array("Benz","ben"),
        array("Germany","due")
    ),
    array(
        array("BMW","bmw"),
        array("Germany","due")
    ),
    array(
        array("Toyota","toy"),
        array("Japan","jap")
    )
);

$cars[0][0][] = "Luxury";
$cars[0][0]['model'] = "2022";

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=2)
      0 => 
        array (size=4)
          0 => string 'Ferrari' (length=7)
          1 => string 'fer' (length=3)
          2 => string 'Luxury' (length=6)
          'model' => string '2022' (length=4)
      1 => 
        array (size=2)
          0 => string 'Italy' (length=5)
          1 => string 'it' (length=2)
  1 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'Benz' (length=4)
          1 => string 'ben' (length=3)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)
  2 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'BMW' (length=3)
          1 => string 'bmw' (length=3)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)
  3 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'Toyota' (length=6)
          1 => string 'toy' (length=3)
      1 => 
        array (size=2)
          0 => string 'Japan' (length=5)
          1 => string 'jap' (length=3)

With every level of the dimensions, you have to add extra brackets to your code.

Removing elements from a PHP multidimensional array

To remove an element from a multidimensional array, you can use the unset() function.

1- Indexed array

$cars = array("Ferrari","Benz","BMW","Toyota");

unset($cars[2]);

var_dump($cars);

Output

array (size=3)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  3 => string 'Toyota' (length=6)

In the example, index 2 is missing from the keys. Note that the unset() function doesn’t change the array’s keys. To reindex the multidimensional array key, you can use the array_values() function in PHP.

$cars = array("Ferrari","Benz","BMW","Toyota");

unset($cars[2]);
$cars = array_values($cars);

var_dump($cars);

Output

array (size=3)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'Toyota' (length=6)

The keys are reordered after the deletion. The PHP array_values() function only works on indexed multidimensional arrays because the associative array doesn’t have ordered keys.

Two-dimensional array:

$cars = array(
    array("Ferrari","Italy"),
    array("Benz","Germany"),
    array("BMW","Germany"),
    array("Toyota","Japan")
);

unset($cars[0][1]);

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=1)
      0 => string 'Ferrari' (length=7)
  1 => 
    array (size=2)
      0 => string 'Benz' (length=4)
      1 => string 'Germany' (length=7)
  2 => 
    array (size=2)
      0 => string 'BMW' (length=3)
      1 => string 'Germany' (length=7)
  3 => 
    array (size=2)
      0 => string 'Toyota' (length=6)
      1 => string 'Japan' (length=5)

Three-dimensional:

$cars = array(
    array(
        array("Ferrari","fer"),
        array("Italy","it")
    ),
    array(
        array("Benz","ben"),
        array("Germany","due")
    )
);

unset($cars[0][0][1]);

var_dump($cars);

Output

array (size=2)
  0 => 
    array (size=2)
      0 => 
        array (size=1)
          0 => string 'Ferrari' (length=7)
      1 => 
        array (size=2)
          0 => string 'Italy' (length=5)
          1 => string 'it' (length=2)
  1 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'Benz' (length=4)
          1 => string 'ben' (length=3)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)

2- Associative array

Deleting an item from the multidimensional associative array is like an indexed array. In the indexed array, we use the index as a key and in the associative array, we use the items key.

$cars = array(
    array(
        array("Ferrari","fer"),
        array("Italy","it")
    ),
    array(
        array("Benz","ben"),
        array("Germany","due")
    ),
    array(
        array("BMW","bmw"),
        array("Germany","due")
    ),
    array(
        array("Toyota","toy"),
        array("Japan","jap")
    )
);

unset($cars[3]);
unset($cars[2][0]);
unset($cars[1][0][0]);

var_dump($cars);

Output

array (size=3)
  0 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'Ferrari' (length=7)
          1 => string 'fer' (length=3)
      1 => 
        array (size=2)
          0 => string 'Italy' (length=5)
          1 => string 'it' (length=2)
  1 => 
    array (size=2)
      0 => 
        array (size=1)
          1 => string 'ben' (length=3)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)
  2 => 
    array (size=1)
      1 => 
        array (size=2)
          0 => string 'Germany' (length=7)
          1 => string 'due' (length=3)

Sorting a multidimensional array

To sort a multidimensional array, you use the sort() function for sorting by value and asort() function to sort by key. For example:

$cars = array(
    "fer" => 
        array(
            "b" => array("aa" => "Ferrari","ab" => "fer"),
            "a" => array("ba" => "Italy","bb" => "it")
        ),
    "ben" => 
        array(
            "a" => array("aa" => "Benz","ab" => "ben"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "bmw" => 
        array(
            "a" => array("aa" => "BMW","ab" => "bmw"),
            "b" => array("ba" => "Germany","bb" => "due")
        ),
    "toy" => 
        array(
            "a" => array("aa" => "Toyota","ab" => "toy"),
            "b" => array("ba" => "Japan","bb" => "jap")
    )
);

sort($cars);

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=2)
      'a' => 
        array (size=2)
          'aa' => string 'BMW' (length=3)
          'ab' => string 'bmw' (length=3)
      'b' => 
        array (size=2)
          'ba' => string 'Germany' (length=7)
          'bb' => string 'due' (length=3)
  1 => 
    array (size=2)
      'a' => 
        array (size=2)
          'aa' => string 'Benz' (length=4)
          'ab' => string 'ben' (length=3)
      'b' => 
        array (size=2)
          'ba' => string 'Germany' (length=7)
          'bb' => string 'due' (length=3)
  2 => 
    array (size=2)
      'a' => 
        array (size=2)
          'aa' => string 'Toyota' (length=6)
          'ab' => string 'toy' (length=3)
      'b' => 
        array (size=2)
          'ba' => string 'Japan' (length=5)
          'bb' => string 'jap' (length=3)
  3 => 
    array (size=2)
      'b' => 
        array (size=2)
          'aa' => string 'Ferrari' (length=7)
          'ab' => string 'fer' (length=3)
      'a' => 
        array (size=2)
          'ba' => string 'Italy' (length=5)
          'bb' => string 'it' (length=2)