猿问

根据模式排列 PHP 数组

鉴于闲置的 $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_id_keep] 与每个 [location_id_delete] 分组,从而为每个 [location_id_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"];

                    }

                }

            }


ITMISS
浏览 143回答 1
1回答

叮当猫咪

$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);
随时随地看视频慕课网APP
我要回答