PHP 遍历多维数组,将多维数组的val 按逗号拼接成字符串

2020-09-13  本文已影响0人  骑蚂蚁上高速_jun

通过 php 自身的迭代器 RecursiveIteratorIterator 实现

$arr = [
    [
        "b",
        1,
        2,
        3,
    ],
    [
        4,
        5,
        "a",
        6,
    ],
    [
        1,
        [
            7,8,9
        ],
    ],
];

$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));
$str = "";
foreach ($iterator as $key => $val) {
    $str .= $val.",";   
}

var_dump(trim($str,","));

// 输出结果
// string(23) "b,1,2,3,4,5,a,6,1,7,8,9"
上一篇下一篇

猜你喜欢

热点阅读