【问题标题】:How to search recursively by key of a multi-dim array?如何通过多维数组的键递归搜索?
【发布时间】:2018-01-10 07:11:23
【问题描述】:

我正在尝试创建一个递归函数,它接受一个数组并查找属性名称children,并从匹配的数组中构造一个数组。

这不是直截了当,因为我不知道我的 JSON 数据的哪个块将包含密钥 children,所以我决定编写一个递归函数。


我试过了

$testDataJson = '
{
    "macAddress": "10:20:30:40:50:81",
    "type": "HGW",
    "children": [{
        "macAddress": "98:D6:D6:D8:FF:34",
        "pendingMethods": false,
        "lastSeen": "2017-05-24T10:36:35",
        "lastSeenLight": "GREEN",
        "model": "AP7465CE-TN",
        "type": "WIRELESS_ACCESS_POINT"
    }, {
        "macAddress": "44:66:E9:A1:2C:DC",
        "pendingMethods": false,
        "lastSeen": "2017-05-24T10:39:01",
        "lastSeenLight": "GREEN",
        "model": "PLC 200+ DIV -TN",
        "type": "POWERLINE"
    }, {
        "macAddress": "D8:C2:A9:1C:44:47",
        "pendingMethods": "False",
        "lastSeen": "2017-05-24T10:39:01",
        "lastSeenLight": "GREEN",
        "model": "PG9073",
        "type": "POWERLINE",
        "children": [{
            "macAddress": "22:CD:E6:8F:8C:B8",
            "pendingMethods": false,
            "lastSeen": "2017-05-24T10:38:16",
            "lastSeenLight": "GREEN",
            "model": "PG9073",
            "type": "POWERLINE"
        }, {
            "macAddress": "13:E4:AB:33:36:AC",
            "pendingMethods": false,
            "lastSeen": "2017-05-24T10:29:13",
            "lastSeenLight": "GREEN",
            "model": "PG9072",
            "type": "POWERLINE_WIRELESS_ACCESS_POINT"
        }]
    }]
}';
$testDataArray = json_decode($testDataJson,true);

function recursiveKeyFinder($array) {
    $result = [];
    if (!isset($array['children']) AND is_array($array)) {
       return $result;
    }else {
        foreach($array['children'] as $child){
            $result['macAddress'] = $child['macAddress'];
        }
        return recursiveKeyFinder($array);
    }
}
var_dump(recursiveKeyFinder($testDataArray));

结果:var_dump() 没有任何结果。


想要的结果:

["macAddress": "98:D6:D6:D8:FF:34",
"macAddress": "44:66:E9:A1:2C:DC",
"macAddress": "D8:C2:A9:1C:44:47",
"macAddress": "22:CD:E6:8F:8C:B8",
"macAddress": "13:E4:AB:33:36:AC"]

我该如何调查?

【问题讨论】:

  • 你有无限递归。您正在使用给定的相同数组进行递归调用。您也永远不会返回您在foreach 循环中修改的$result
  • 你想要得到什么结果?
  • 现在开车回家会在我回家后立即更新。我正在尝试返回我的结果数组并使我的函数工作。我将添加更多详细信息
  • 你不能同时编码和开车吗?你永远不会在这个行业成功。 :)
  • @Barmar :我更新了我的帖子添加了我想要的结果,我试图摆脱我的故障递归函数。

标签: php arrays json search recursion


【解决方案1】:

就像 Barmar siad “你有无限递归。”

这是我的解决方案。它打印出所有的mac地址

function recursiveKeyFinder($array) {
    $result = [];

    $result[] = $array['macAddress'];
    if (isset($array['children'])) {
        foreach($array['children'] as $child){
            $result = array_merge($result,recursiveKeyFinder($child));
        }
    }
    return $result;
}

这里是结果

array (size=6)
  0 => string '10:20:30:40:50:81' (length=17)
  1 => string '98:D6:D6:D8:FF:34' (length=17)
  2 => string '44:66:E9:A1:2C:DC' (length=17)
  3 => string 'D8:C2:A9:1C:44:47' (length=17)
  4 => string '22:CD:E6:8F:8C:B8' (length=17)
  5 => string '13:E4:AB:33:36:AC' (length=17)

希望对你有帮助

【讨论】:

    【解决方案2】:

    PHP 已经为此操作提供了一个特定的工具:array_walk_recursive();所以没有必要重新发明轮子。

    准备好 json 数据后,只需一个函数调用即可快速、简洁地完成您的任务。

    省略第一个 macAddress(不需要的)值是通过仅将具有 'children' 键的子数组传递给 array_walk_recursive() 来完成的。

    代码:(Demo)

    $array=json_decode($testDataJson,true)['children'];  // this avoids including the first "non-children" macAddress value.
    array_walk_recursive($array,function($v,$k)use(&$result){if($k==='macAddress') $result[]=$v;});
    var_export($result);
    

    结果:

    array (
      0 => '98:D6:D6:D8:FF:34',
      1 => '44:66:E9:A1:2C:DC',
      2 => 'D8:C2:A9:1C:44:47',
      3 => '22:CD:E6:8F:8C:B8',
      4 => '13:E4:AB:33:36:AC',
    )
    

    或者,输入数组可以这样准备:

    $array=array_diff_key(json_decode($testDataJson,true),['macAddress'=>'']);
    

    这将确保您不会意外抓取任何非子 macAddress 值。

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 2014-05-20
      • 2019-10-16
      • 2011-04-27
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2012-06-10
      相关资源
      最近更新 更多