鑒于閑置的 $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 ) ))我想將像休閑模式這樣的元素分組到結(jié)果: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 ))規(guī)則應(yīng)該是這樣的,對(duì)于輸入數(shù)組中的每個(gè)數(shù)組,將 [location_id_keep] 與每個(gè) [location_id_delete] 分組,從而為每個(gè) [location_id_delete] 生成一個(gè)新數(shù)組,就像在所需的輸出示例中一樣。到目前為止我嘗試過(guò)的: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"]; } } }
1 回答

叮當(dāng)貓咪
TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
$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);
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)
0/150
提交
取消