基于模式的PHP数组排列
给定空闲的$array:基于模式的PHP数组排列,php,arrays,sorting,Php,Arrays,Sorting,给定空闲的$array: Array ( [0] => Array ( [location_id_keep] => 25 [location_id_delete] => 1 ) [1] => Array ( [location_id_keep] => 26 [location_id_delete] => Array (
Array (
[0] => Array
(
[location_id_keep] => 25
[location_id_delete] => 1
)
[1] => Array
(
[location_id_keep] => 26
[location_id_delete] => Array
(
[0] => 3
[1] => 4
)
)
)
我想对元素进行分组,如休闲模式,以获得以下结果:
Array (
[0] => Array
(
[location_id_keep] => 25
[location_id_delete] => 1
)
[1] => Array
(
[location_id_keep] => 26
[location_id_delete] => 3
)
[2] => Array
(
[location_id_keep] => 26
[location_id_delete] => 4
)
)
规则应该是这样的,对于输入数组中的每个数组,将[location\u id\u keep]与每个[location\u id\u delete]分组,为每个[location\u id\u delete]生成一个新数组,如所需的输出示例中所示
到目前为止,我尝试的是:
foreach ($array as $id) {
//check if location_id_delete have more than 1 value
if( $id["location_id_delete"][1]) {
foreach($id["location_id_delete"] as $del){
$array["location_id_delete"][] = $del;
$array["location_id_keep"]=id["location_id_keep"];
}
}
}
$output = array();
foreach ($array as $i => $id) {
//check if location_id_delete have more than 1 value
if( is_array($id["location_id_delete"])) {
foreach($id["location_id_delete"] as $del){
$output[] = array(
'location_id_keep' => $array[$i]["location_id_keep"],
'location_id_delete' => $del
);
}
}else{
$output[] = $array[$i];
}
}
var_dump($output);