所以我有一个具有类似元素的数组,我需要将它合并到一个多维数组中,将一些元素合并在一起,我可能会过多地说出生活,但我想我会问......提前谢谢:)
我当前的数组:
Array
(
[0] => Array
(
[name] => Facebook
[icon] =>
[sectors] => BSS
[url] => http://www.facebook.com/...
)
[1] => Array
(
[name] => Facebook
[icon] =>
[sectors] => BSP
[url] => http://www.facebook.com/...
)
[2] => Array
(
[name] => GooglePlus
[icon] =>
[sectors] => BSP
[url] => https://plus.google.com/...
)
[3] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => BSS
[url] => http://www.linkedin.com/...
)
[4] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => BSP
[url] => http://www.linkedin.com/...
)
[5] => Array
(
[name] => Twitter
[icon] =>
[sectors] => BSS
[url] => http://twitter.com/...
)
[6] => Array
(
[name] => Twitter
[icon] =>
[sectors] => BSP
[url] => http://twitter.com/...
)
[7] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => BSS
[url] => http://vimeo.com/...
)
[8] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => BSP
[url] => https://vimeo.com/...
)
[9] => Array
(
[name] => Youtube
[icon] =>
[sectors] => BSS
[url] => http://www.youtube.com/...
)
[10] => Array
(
[name] => Blog
[icon] =>
[sectors] => Local
[url] => /blog
)
)
我最终需要:
Array
(
[0] => Array
(
[name] => Facebook
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.facebook.com/...
)
[1] => Array
(
[name] => BSP
[url] => http://www.facebook.com/...
)
)
)
[1] => Array
(
[name] => GooglePlus
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSP
[url] => https://plus.google.com/...
)
)
)
[2] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.linkedin.com/....
)
[1] => Array
(
[name] => BSP
[url] => http://www.linkedin.com/...
)
)
)
[3] => Array
(
[name] => Twitter
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://twitter.com/...
)
[1] => Array
(
[name] => BSP
[url] => http://twitter.com/...
)
)
)
[4] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://vimeo.com/...
)
[1] => Array
(
[name] => BSP
[url] => https://vimeo.com/...
)
)
)
[5] => Array
(
[name] => Youtube
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.youtube.com/....
)
)
)
[6] => Array
(
[name] => Blog
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => Local
[url] => /blog
)
)
)
)
答案 0 :(得分:1)
这就是我要做的事情:
foreach ($original_array as $value){
$result_array[$value['name']]['name'] = $value['name'];
$result_array[$value['name']]['icon'] = $value['icon'];
$result_array[$value['name']]['sectors'][] = array(
'name' => $value['sectors'],
'url' => $value['url']
}
我正在做的是使用名称(Facebook,Youtube等)作为数组键,以便能够将(使用[])子数组的扇区和url元素推送到它们自己的子数组中
从这里你可以按原样返回$ result_array,或者你真的需要一个索引数组,返回array_values($ result_array)
我假设图标在两个具有相同名称的条目之间不会改变。如果不是这种情况,您可以创建名称+图标的哈希,或者只是连接名称和图标,并将其用作数组键。