5

I have 2 arrays that I'm working with. The first array comes from the data of a CSV file and the other is a response from an API.

Is it possible to filter array 2 by using matching values from array 1?

Array 1 Example

[
    ["B00CEEZ57S"],
    ["B002QJZADK"],
    ["B001EHL2UK"],
    ["B003FSTNB6"],
]

Array 2 Example

[
    [
        "name" => "Jonathan Franzen: Purity (Hardcover); 2015 Edition",
        "ASIN" => "B002QJZADK"
    ],
    [
        "name" => "Cardinal Gates Outdoor Child Safety Gate, Brown",
        "ASIN" => "B00CE8C7SO"
    ],
    [
        "name" => "Sauder Edge Water 71.88\" Bookcase Estate Black Finish",
        "ASIN" => "B001EHL2UK"
    ],
    [
        "name" => "The Pioneer Woman 82695.03R Cowboy Rustic 8\" Rosewood Handle Can Opener, Scis...",
        "ASIN" => "B015LU7GPU"
    ]
]

These rows should be kept:

[
    'name' => 'Jonathan Franzen: Purity (Hardcover); 2015 Edition',
    'ASIN' => 'B002QJZADK',
],
[
    'name' => 'Sauder Edge Water 71.88" Bookcase Estate Black Finish',
    'ASIN' => 'B001EHL2UK'
]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
mattchambers
  • 185
  • 3
  • 15

3 Answers3

9

Assuming the two arrays as $array1 and $array2 respectively, the following steps need to be followed:

  1. Convert $array1 to a 1 dimensional array $options because it's easier to check for values that way.
  2. Filter $array2 using array_filter() such that the value corresponding to 'ASIN' index matches with values contained in $options

    foreach ($array1 as $arr) {
        $options[] = current($arr);  // COnverted to 1-d array
        /* Result:  Array ( [0] => B00CEEZ57S [1] => B002QJZADK [2] => B001EHL2UK [3] => B003FSTNB6 )*/
    }
    
    /* Filter $array2 and obtain those results for which ['ASIN'] value matches with one of the values contained in $options */
    $result = array_filter($array2, function($v) use ($options) {
        return in_array($v['ASIN'], $options);
    });
    
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
2

please try this one.

<?php
$arr = Array
(
    Array(
        "name" => "Jonathan Franzen: Purity (Hardcover); 2015 Edition",
        "ASIN" => "B002QJZADK"
    ),
    Array(
        "name" => "Cardinal Gates Outdoor Child Safety Gate, Brown",
        "ASIN" => "B00CE8C7SO"
    ),
    Array(
        "name" => "Sauder Edge Water 71.88' Bookcase Estate Black Finish",
        "ASIN" => "B001EHL2UK"
    )
);

$arr2 = Array(
    Array("B00CEEZ57S"),
    Array("B002QJZADK"),
    Array("B001EHL2UK")
);

$arr2Make = array();
foreach ($arr2 as $key => $a) {
    if ( isset($a[0]) ) {
        $arr2Make[] = $a[0];
    }
}


$arrMake = array_filter($arr,function($a){
    global $arr2Make;
    if ( in_array($a["ASIN"], $arr2Make) ) {
        return $a;
    }
});


print_r($arrMake);
?>
ABHIJIT
  • 627
  • 4
  • 16
0

It is not necessary to prepare the datasets at all.

Use a custom callback while searching for intersecting data points.

Do not think of $a and $b as being respectively from $array2 and $array1 -- these rows of data may come from either array as the function sorts and compares the data.

Code: (Demo)

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

Output:

array (
  0 => 
  array (
    'name' => 'Jonathan Franzen: Purity (Hardcover); 2015 Edition',
    'ASIN' => 'B002QJZADK',
  ),
  2 => 
  array (
    'name' => 'Sauder Edge Water 71.88" Bookcase Estate Black Finish',
    'ASIN' => 'B001EHL2UK',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136