PHP_version = 7.4.1
遍历嵌套数组的函数:
<?php $embed_array = [ "name" => [ "TangSeng", "SunWukong", "ShaWujing" ], "work" => [ "none1", "none2", "none3" ], "files" => [ "file1", "file2", "file3" =>[ "I am a five ...", "I am a five ..., too ", "I am a five ..., last" ] ] ]; echo $embed_array["name"][2] .PHP_EOL; var_dump($embed_array); function travel_array($array){ $sep = ""; if (is_array($array)){ foreach($array as $key=>$value){ if(is_array($value)){ travel_array($value); } else { $sep = $sep . " "; echo $sep. $key . "=>". $value .PHP_EOL; } } } else { echo $sep . $array .PHP_EOL; } } travel_array($embed_array); ?> ShaWujing array(3) { ["name"]=> array(3) { [0]=> string(8) "TangSeng" [1]=> string(9) "SunWukong" [2]=> string(9) "ShaWujing" } ["work"]=> array(3) { [0]=> string(5) "none1" [1]=> string(5) "none2" [2]=> string(5) "none3" } ["files"]=> array(3) { [0]=> string(5) "file1" [1]=> string(5) "file2" ["file3"]=> array(3) { [0]=> string(15) "I am a five ..." [1]=> string(21) "I am a five ..., too " [2]=> string(21) "I am a five ..., last" } } } // output 0=>TangSeng 1=>SunWukong 2=>ShaWujing 0=>none1 1=>none2 2=>none3 0=>file1 1=>file2 0=>I am a five ... 1=>I am a five ..., too 2=>I am a five ..., last
function print_array(array $in){ foreach($in as $key=>$value){ echo " $value"; } echo " ".PHP_EOL; }
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/15622550.html