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

使用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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题