简体   繁体   English

遍历php多级关联数组

[英]Loop through php multi level associative array

I am struggling to return values from all layers of a multidimensional array, eg value then all child elements. 我正在努力从多维数组的所有层返回值,例如,然后返回所有子元素的值。 I've tried lots of loops but can't quite get it right. 我已经尝试了很多循环,但不能完全正确。 Please can someone help - I've been stuck for ages! 请有人帮忙-我已经坚持了很长时间! Thank you. 谢谢。

The array is: 该数组是:

Array
(
    [SiteRep] => Array
        (   [DV] => Array
                (
                    [Location] => Array
                        (
                            [Period] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => 2016-12-19Z
                                            [Rep] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [D] => W
                                                            [F] => 1
                                                            [G] => 9
                                                        )

                                                    [1] => Array
                                                        (
                                                            [D] => W
                                                            [F] => 0
                                                            [G] => 7
                                                        )

                                                )
                                        )

                                    [1] => Array
                                        (
                                            [value] => 2016-12-20Z
                                            [Rep] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [D] => ENE
                                                            [F] => 4
                                                            [G] => 7

                                                        )

                                                    [1] => Array
                                                        (
                                                            [D] => E
                                                            [F] => 3
                                                            [G] => 9

                                                        )

so far my code is: 到目前为止,我的代码是:

$i=0;
    foreach ($json_decoded['SiteRep']['DV']['Location']['Period'][$i] as $key => $value) {
    if (is_array($value)){
        foreach ($value as $key2 => $value2){
            if (is_array($value2)){
                foreach ($value2 as $key3 => $value3) {
                    echo $key3 . " 3: " . $value3 . "<br>";
                }
            } else {
                echo $key2 . " 2: " . $value2 . "<br>";
            }
        };

    } else {
        echo $key . " 1: " . $value . "<br>";
    }
    $i++;
};

I believe you are looking for recursion. 我相信您正在寻找递归。 A function that calls itself. 调用自身的函数。 It can be tricky but it is by far the most efficient way to navigate an array of indeterminate depth. 这可能很棘手,但它是迄今为止导航不确定深度数组的最有效方法。

function printStuff($stuff)
{
  echo $stuff['value']
  if (isset($stuff['rep']))
  {
    printStuff($stuff['rep']);
  }
}

If you want the values to pass down simply change echo to a return value: 如果您希望这些值向下传递,只需将echo更改为返回值即可:

function printStuff($stuff)
{
  $temp = $stuff['value']
  if (isset($stuff['rep']))
  {
    $temp .= printStuff($stuff['rep']);
  }
  return $temp;
}

Note: recursion can cause out of memory if not set up correctly similar to an infinite loop, as the recursive function call pushes the function call onto the call stack every time. 注意:如果没有像无限循环那样正确设置,则递归会导致内存不足,因为递归函数调用每次都会将函数调用推入调用堆栈。 Just make sure your function call has a subset of the parameter passed in. 只要确保您的函数调用具有传入的参数的子集即可。

If your array has the constantly number of layers, you may use foreach-function for the each layer. 如果阵列的层数恒定,则可以对每一层使用foreach函数。 If your array has different number of layers every time - you should use the recursion, because it's the only solution in your situation. 如果每次阵列的层数不同,则应使用递归,因为这是您情况下的唯一解决方案。

Something like this: 像这样:

array_walk_recursive($array, function($item, $key){
    //your actions
});

I've continued drilling down and managed to get all the data I need by using... 我一直在深入研究,并设法通过使用...获取了我需要的所有数据。

foreach ($json_decoded['SiteRep']['DV']['Location']['Period'] as $key => $value) {
if (is_array($value)){
    foreach ($value as $key2 => $value2){
        if (is_array($value2)){
            foreach ($value2 as $key3 => $value3) {
                foreach ($value3 as $key4 => $value4) {
                    echo $key4 . ": " . $value4 . "<br>";
                }
            }
        } else {
            echo $key2 . ": " . $value2 . "<br>";
        }
    };
} else {
    echo $key . ": " . $value . "<br>";
}
$i++;

}; };

This can map the array recursivly. 这样可以递归地映射数组。 Preserving they keys and structure. 保留它们的键和结构。 You can however only return a new value and not change the key. 但是,您只能返回一个新值,而不能更改密钥。 But you will know which key you are looking at and if you just want to echo it you can do that to. 但是您会知道您正在查看哪个键,如果您只想回显它,则可以这样做。

array_map_assoc_recursive('callback', $array)

function callback($key, $value) {
    return "new" . $value;
}


function array_map_assoc_recursive($f, array $a) {
    return array_column(array_map(function ($key, $value) use ($f) {
        if (is_array($value)) {
            return [$key, array_map_assoc_recursive($f, $value)];
        } else {
            return [$key, call_user_func($f, $key, $value)];
        }
    }, array_keys($a), $a), 1, 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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