Search code snippets, questions, articles...

Multidimentional array

<?php
$cars = array(
            'model' => array(
                        1 => 'BMW',
                        2 => 'AUDI',
                        3 => 'Bentley'
                    ),
            'colour' => array(
                        'BMW' => 'Red',
                        'Audi' => 'Black',
                        'Bently' => 'Blue'
                    ),
            'doors' => array(
                        'BMW' => 4,
                        'Audi' => 5,
                        'Bently' => 2
                    ),
        ); 

        print_r($cars);
     
        //echo 'I love my ' . $cars['model'][1] . ' but the it is ' . $cars['colour']['BMW'] 
              //. ' and only a ' . $cars['doors']['BMW'] . ' door.';


        $keys = array_keys($cars);
        for($i = 0; $i < count($cars); $i++) {
            echo $keys[$i] . "{<br>";
            foreach($cars[$keys[$i]] as $key => $value) {
                echo $key . " : " . $value . "<br>";
            }
            echo "}<br>";
        }
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

multidimensional

array

output

loop

Search Index Data (The code snippet can also be found with below search text)

multidimentional
array
output
loop
Was this helpful?
0 Comments