简体   繁体   中英

PHP echo associative multi-dimensional array

I am trying to create a multi-dimensional associative array and display all of its values.

How do I do that using for loop?

Similar code:

$StudScore = array( 
    "Mary" => array(
        "physics" => 35,        
        "maths" => 30,      
        "chemistry" => 39       
    ),
    "Tom" => array(
        "physics" => 30,
        "maths" => 32,
        "chemistry" => 29
    ),
    "Jon" => array(
        "physics" => 31,
        "maths" => 22,
        "chemistry" => 39
    )
);

Look at this recursive function and see if it fits:

function echoArray($array) {
    foreach ($array as $key => $value) {
        echo "<li>$key</li>";
        if (is_array($value)) {
            echo "<ul>";
            echoArray($value);
            echo "</ul>";
        } else {
            echo "<ul><li>$value</li></ul>";
        }
    }
}

Change the way you display each item as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM