1
我有一個數組,其中包含來自數據庫的一組信息,但其中一些數據是重複的,只有數組最深處的值爲1 。例如:PHP從多維數組中找到值,然後將它們放在一個數組中
[0] => Array(['id'] => 1 , ['name'] => "First Array" , ['more'] => "stuff")
[1] => Array(['id'] => 2 , ['name'] => "Second Array" , ['more'] => "stuff1")
[2] => Array(['id'] => 2 , ['name'] => "Second Array" , ['more'] => "stuff2")
[3] => Array(['id'] => 3 , ['name'] => "Third Array" , ['more'] => "stuff3")
[4] => Array(['id'] => 3 , ['name'] => "Third Array" , ['more'] => "stuff4")
我想要做的就是用相同的[「身份證」]字段中的任何陣列到陣列的陣列,而不是[「更多」]。例如:
[0] => Array(['id'] => 1 , ['name'] => "First Array" , ['more'] => "stuff")
[1] => Array(['id'] => 2 , ['name'] => "Second Array" , ['more'] => Array([0] => "stuff1" , [1] => "stuff2"))
[2] => Array(['id'] => 2 , ['name'] => "Third Array" , ['more'] => Array([0] => "stuff3" , [1] => "stuff4"))
我試圖做這幾種方法,但我可以得到最接近的是這將會把所有的[「更多」]值到1門陣列這樣的功能:
[0] => Array(['id'] => 1 , ['name'] => "First Array" , ['more'] => "stuff")
[1] => Array(['id'] => 2 , ['name'] => "Second Array" , ['more'] => Array([0] => "stuff1" , [1] => "stuff2" , [2] => "stuff3" , [3] => "stuff4"))
這就是從這樣的功能:
<?php
private function compress_duplicates($a = array()) {
$diff_key = array_diff_key($a , array_unique($a));
$first_key = array_shift(array_keys($diffkey));
$array["id"] = $diff_key[$first_key]["id"];
$array["name"] = $diff_key[$first_key]["name"];
foreach($diff_key as $term) {
$array["more"][] = $term["more"];
}
return $array;
}
?>
基本上我問的:我怎樣才能找到一個多維數組複製,然後將每個這些重複的到自己的陣列?