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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里