2016-03-02 100 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