donglingyi4679 2016-09-16 09:44
浏览 137
已采纳

使用PHP在另一个数组中按值过滤数组

I have an array like this:

[
  {
    "Firepack_sn":"20012205",
    "Installation_Date":"",
    "Type":"EH","Standard":"VAS",
    "Capacity_m3h":"81",
    "Pressure_bar":"3,4",
    "Rpm":"2930",
    "Power_kw":"72"
  },
  {
    "Firepack_sn":"20023901",
    "Installation_Date":"",
    "Type":"DH","Standard":"VAS",
    "Capacity_m3h":"195",
    "Pressure_bar":"4,2",
    "Rpm":"2000",
    "Power_kw":"72"
  }
]

And an array2 like this:

[
  {
    "user_id":"40009",
    "firepack_id":"20012205",
    "activated":"1"
  },
  {
    "user_id":"40009",
    "firepack_id":"21020393",
    "activated":"0"
  }
]

Now I want to filter the first array, so I only get the element with Firepack_sn that exists in array2 as firepack_id

So i want the output to be like this:

[
  {
    "Firepack_sn":"20012205",
    "Installation_Date":"",
    "Type":"EH","Standard":"VAS",
    "Capacity_m3h":"81",
    "Pressure_bar":"3,4",
    "Rpm":"2930",
    "Power_kw":"72"
  }
]

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • duanmu8911 2016-09-16 09:53
    关注

    Here is a way to do it :

    First, you need to extract the firepack_id you need to look for, then you need to loop through $arr1 and check if Firepack_sn is the same than than one of the firepack_id you extracted before, if yes, then you add it to an array.

    $arr1 = json_decode('[{"Firepack_sn":"20012205","Installation_Date":"","Type":"EH","Standard":"VAS","Capacity_m3h":"81","Pressure_bar":"3,4","Rpm":"2930","Power_kw":"72","Pump_Type":"KSB KFP50-200","Motor_Type":"DOOSAN PU066 VAS/CEA","Controller_Type":"WB882-E10 VAS","Pump_sn":"085259","Motor_sn":"EARPA209635","Controller_sn":"","Servicelevel":"","Cust_id":"0","Cust_branche":"","Cust_name":"","Cust_address1":"","Cust_zipcode":"","Cust_city":"","Cust_country":"","Cust_Phone":"","Cust_coachlevel":"","Site_Name":"E-set","Site_address1":"","Site_address2":"","Site_address3":"","Site_zipcode":"","Site_city":"","Site_country":"","Site_contact":"","Site_phone":"","activated":"1"},{"Firepack_sn":"20023901","Installation_Date":"","Type":"DH","Standard":"VAS","Capacity_m3h":"195","Pressure_bar":"4,2","Rpm":"2000","Power_kw":"72","Pump_Type":"KSB KFP50-200","Motor_Type":"DOOSAN PU066 VAS/CEA","Controller_Type":"WB882-E10 VAS","Pump_sn":"085259","Motor_sn":"EARPA209635","Controller_sn":"","Servicelevel":"","Cust_id":"0","Cust_branche":"","Cust_name":"","Cust_address1":"","Cust_zipcode":"","Cust_city":"","Cust_country":"","Cust_Phone":"","Cust_coachlevel":"","Site_Name":"D-set","Site_address1":"","Site_address2":"","Site_address3":"","Site_zipcode":"","Site_city":"","Site_country":"","Site_contact":"","Site_phone":"","activated":"0"}]');
    
    $arr2 = json_decode('[{"user_id":"40009","firepack_id":"20012205","activated":"1"},{"user_id":"40009","firepack_id":"21020393","activated":"0"}]');
    
    $firepackIds = array();
    foreach($arr2 as $item){
      $firepackIds[] = $item->firepack_id;
    }
    
    $goodRows = array();
    foreach($arr1 as $item){
      if(in_array($item->Firepack_sn, $firepackIds)){
        $goodRows[] = $item;
      }
    }
    
    echo json_encode($goodRows);
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作