dt2015 2017-08-02 21:32
浏览 61
已采纳

如何通过多维数组的键递归搜索?

I am trying to create a recursive function that takes an array and looks for a property name children, and constructs an array out of the matching one.

This is not straight forward because I don't know which block of my JSON data will contain the key children, so I decided to write a recursive function.


I've tried

$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));

Result: Nothing from var_dump().


Desired result:

["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"]

Any hints/suggestions / helps on this be will be much appreciated!

  • 写回答

2条回答 默认 最新

  • dpmpa26468 2017-08-02 21:56
    关注

    Like Barmar siad "You have infinite recursion."

    This is my solution. It prints out all mac address

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

    Here the 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)
    

    Hope this can help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里