-5

I have an array like this:

$array1 = [
    [
        'Firepack_sn' => '20012205',
        'Installation_Date' => '',
        'Type' => 'EH',
        'Capacity_m3h' => '81',
        'Pressure_bar' => '3,4',
        'Rpm' => '2930',
        'Power_kw' => '72',
    ],
    [
        'Firepack_sn' => '20023901',
        'Installation_Date' => '',
        'Type' => 'DH',
        'Capacity_m3h' => '195',
        'Pressure_bar' => '4,2',
        'Rpm' => '2000',
        'Power_kw' => '72',
    ],
];

And an array2 like this:

$array2 = [
    [
        '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 rows with a Firepack_sn value that exists in array2 as firepack_id.

Desired output:

[
  {
    "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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
DNC
  • 443
  • 1
  • 6
  • 20

2 Answers2

1

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.

vincenth
  • 1,732
  • 5
  • 19
  • 27
  • Thank you! your answer didn't work, but with some small edits it worked. the edits: $item->firepack_id; = $item['firepack_id'] $item->Firepack_sn = $item['Firepack_sn'] – DNC Sep 16 '16 at 10:10
  • @DNC what wasn't working. I have some difficulty understanding your edits, I suspect some part of them were stripped, could you post a gist somewere so that I can update what I posted ? – vincenth Sep 16 '16 at 10:13
0

You do not need multiple loops or inefficient iterated in_array() calls. PHP already offers a native function to compare rows between multiple 2D arrays -- array_uintersect(). Because the custom callback uses rows from either array while making comparisons, you must build the callback's logic to fallback to potentially use either array's target column value.

Code: (Demo)

var_export(
    array_uintersect(
        $array1,
        $array2,
        fn($a, $b) =>
            ($a['Firepack_sn'] ?? $a['firepack_id'])
            <=>
            ($b['Firepack_sn'] ?? $b['firepack_id'])
    )
);

Output:

array (
  0 => 
  array (
    'Firepack_sn' => '20012205',
    'Installation_Date' => '',
    'Type' => 'EH',
    'Capacity_m3h' => '81',
    'Pressure_bar' => '3,4',
    'Rpm' => '2930',
    'Power_kw' => '72',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136