为了账号安全,请及时绑定邮箱和手机立即绑定

根据模式排列 PHP 数组

根据模式排列 PHP 数组

PHP
ITMISS 2022-01-02 13:54:06
鉴于闲置的 $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"];                    }                }            }
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

$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);


查看完整回答
反对 回复 2022-01-02
  • 1 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号