如何让有1-N含量PHP中的multidmentional阵列的叶节点值 [英] how to get the leaf node values of a multidmentional array which has 1-N levels PHP

查看:123
本文介绍了如何让有1-N含量PHP中的multidmentional阵列的叶节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组:

Array
(
    [23] => 540,
    [25] => Array
        (
            [656] => Array(671 ,680),
            [345] => 400
        )
)

我想要得到的值:

I want to get the values:

540 , 671 ,680 , 400

对于我的问题是,这个阵列的增长,不知道有多少层次深,

The problem for me is, that this array is growing and don't know how many levels deep,

推荐答案

您可以使用SPL公司的 RecursiveArrayIterator 和的 RecursiveIteratorIterator LEAVES_ONLY 标记。

You can use SPL's RecursiveArrayIterator and RecursiveIteratorIterator with its LEAVES_ONLY flag.

自足例如:

<?php
$rai = new RecursiveArrayIterator(getData());
// $rii = new RecursiveIteratorIterator($rai, RecursiveIteratorIterator::LEAVES_ONLY) - but this is the default
$rii = new RecursiveIteratorIterator($rai);
foreach($rii as $n) {
    echo $n, "\n";
}

// test data source    
function getData() {
    return array(
        23 => 540,
            25 => array(
                656 => array(671,680),
                345 => 400
            )
    );
}

打印

540
671
680
400

这篇关于如何让有1-N含量PHP中的multidmentional阵列的叶节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆