2017-08-02 70 views
0

我想創建一個遞歸函數,它接受一個數組,並尋找屬性名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"] 

任何提示/建議/在這個可以幫助將非常感激!

+2

你有無限遞歸。您正在使用與您相同的數組進行遞歸調用。你也永遠不會返回你在'foreach'循環中修改的'$ result'。 – Barmar

+1

你試圖得到什麼結果? – Barmar

+0

現在開車回家後會儘快更新。我想返回我的結果數組,並使我的功能工作。我會添加更多的細節 – ihue

回答

1

像Barmar西亞德「你有無限遞歸。」

這是我的解決方案。它打印出所有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值。