2016-03-02 145 views
1

我有兩個數組在PHP中,我需要傳遞給一個JS腳本。 PHP的陣列locationsrooms如下:合併PHP數組和嵌套關係

的var_dump($位置)

Array ([0] => Array 
       ([id] => 1 
       [name] => "London" ) 
     [1] => Array 
       ([id] => 2 
       [name] => "Manchester" ) 
    ) 

的var_dump($房)

Array ([0] => Array 
       ([id] => 1 
       [locationId] => "1" 
       [name] => "Lon Room 1" ) 
     [1] => Array 
       ([id] => 2 
       [locationId] => "1" 
       [name] => "Lon Room 2" ) 
     [2] => Array 
       ([id] => 3 
       [locationId] => "1" 
       [name] => "Lon Room 3" ) 
     [3] => Array 
       ([id] => 4 
       [locationId] => "2" 
       [name] => "Man Room 1" ) 
     [4] => Array 
       ([id] => 5 
       [locationId] => "2" 
       [name] => "Man Room 2" ) 
    ) 

我需要客房陣列合併在到適當位置下的位置數組分組中,以便我能夠將以下語法吐出到一個稱爲DailyPlot Sched的JS插件uler。

{ name: "London", id: "1", children:[ 
     { name : "Lon Room 1", id : "1" }, 
     { name : "Lon Room 2", id : "2" }, 
     { name : "Lon Room 3", id : "3" } 
     ] 
}, 
{ name: "Manchester", id: "2", children:[ 
     { name : "Man Room 1", id : "1" }, 
     { name : "Man Room 2", id : "2" } 
     ] 
} 

我在這裏和那裏學習一些東西我學徒的一部分,但我不夠好,由我自己來包裝我解決這個頭,只是還沒有哈哈抱歉,並感謝您!

+0

'$ locations'中的鍵因打字錯誤而不同,或者它們正確? (身份證+姓名,姓名+年齡) – fusion3k

+0

錯別字對不起!好點,我糾正了它。 – Arbiter

+0

我做了大約20次嘗試 - 沒有任何工作,都非常混亂,沒想到會有幫助,對不起。 – Arbiter

回答

2

如果發現您的位置ID索引的數組,那麼你可以使用索引來增加孩子的指定位置:

$locations_by_id = []; 

foreach($locations as $location) { 
    $location['children'] = []; //initialize children to an empty array 
    $locations_by_id[$location['id']] = $location; 
} 

foreach($rooms as $room) {  
    //add room to location 
    $locations_by_id[$room['locationId']]['children'][] = $room; 
} 

$locations = array_values($locations_by_id); //removes the id index 

print json_encode($locations); 
+0

感謝您的支持,我甚至在做出我的嘗試時都沒有考慮到這一點!我選擇接受fusion3k的答案,因爲它似乎更清潔,我喜歡使用array_column和使用array_search索引的創造力。謝謝你的幫助! – Arbiter

+1

@Arbiter其實,FuzzyTree的答案比我的效率更高:) – fusion3k

+0

你能解釋一下爲什麼嗎?是否使用較少的功能?謝謝各位 – Arbiter

1

您可以創建locations陣列的所有id一個數組,那麼你可以直接使用array_search添加每個rooms陣列locations數組:

$index = array_column($locations, 'id'); 
foreach($rooms as $key => $val) 
{ 
    $found = array_search($val['locationId'], $index); 
    $locations[$found]['children'][] = array('name' => $val['name'], 'id' => $val['id']); 
} 

$json = json_encode($locations); 

eval.in demo

+0

真的太棒了,謝謝!很有創意。我之前沒有真正使用過array_column和array_search函數,所以這是一個很酷的例子。我已經用我的數據庫測試過了,它工作的很好,非常漂亮的代碼。 – Arbiter

1

我注意到您的一條評論,您正在使用數據庫,所以我決定繼續並添加一個答案。另一種選擇是通過加入查詢中的位置和房間來避免讓兩個數組開始。我做了一些假設有關表/列的名稱,但像這樣的查詢應該工作:

SELECT l.id AS l_id, l.name AS l_name, r.id AS r_id, r.name AS r_name 
FROM locations l LEFT JOIN rooms r ON l.id = r.locationId 
ORDER BY l.id, r.id 

然後當你從你的結果取行,你可以使用位置ID爲重點構建陣列。

while ($room = $query->fetch()) { // fetch method depends on what db extension you're using 
    $rooms[$room['l_id']]['name'] = $room['l_name']; 
    $rooms[$room['l_id']]['id'] = $room['l_id']; 
    $rooms[$room['l_id']]['children'][] = array('name' => $room['r_name'], 
               'id' => $room['r_id']); 
} 

echo json_encode(array_values($rooms)); 
+0

一個非常酷的主意,謝謝!我會投票的意見和建議,但我還不能:) – Arbiter