简体   繁体   中英

Search for value within an array according to another array that passes one of the values ​within the first array

I tried several ways to make this work but I couldn't, I hope someone here can help me.

The api returns all the values that are inside the array called CODE. I want to get all rgb1 according to the CODE of that array.

For example :

This is array $code :

Array ( [0] => BA [1] => BB [2] => BK [3] => BM [4] => BS [5] => BT [6] => BX [7] => GN [8] => GV [9] => GY [10] => RB [11] => RE [12] => RF [13] => RX [14] => SI [15] => SV [16] => WA [17] => WB [18] => WC )

This is api response json_decode:

[0] => Array ( [vifnum] => 14705 [code] => BA [title] => Apex Blue Pearl [simpletitle] => Blue [rgb1] => 0500C2 [rgb2] => [shotin] => 0 [id] => 3690699 ) 
[1] => Array ( [vifnum] => 14705 [code] => WC [title] => White Diamond Pearl [simpletitle] => White [rgb1] => C9C6BD [rgb2] => [shotin] => 0 [id] => 3690700 )
[2] => Array ( [vifnum] => 14705 [code] => WB [title] => White Diamond Pearl [simpletitle] => White [rgb1] => C9C6BD [rgb2] => [shotin] => 0 [id] => 3690701 )
[3] => Array ( [vifnum] => 14705 [code] => WA [title] => White Diamond Pearl [simpletitle] => White [rgb1] => C9C6BD [rgb2] => [shotin] => 0 [id] => 3690702 )
[4] => Array ( [vifnum] => 14705 [code] => SV [title] => Lunar Silver Metallic [simpletitle] => Silver [rgb1] => A3A4A4 [rgb2] => [shotin] => 0 [id] => 3690703 )
[5] => Array ( [vifnum] => 14705 [code] => SI [title] => Lunar Silver Metallic [simpletitle] => Silver [rgb1] => A3A4A4 [rgb2] => [shotin] => 1 [id] => 3690704 )

This is my code :

for ($ca = 0; $ca < count($image_data['urls']); $ca++) {
  if ($result[$ca] == current($code)) {
    echo $code[$ca] . " COR: " .$response[$ca]['rgb1'] . " INDEX: " . $ca . "<br>";
  } else {
    next($code);
  }
}

But it didn't work, it returns different values than the ones I search for in the code array. I tried it in other ways but I'm not getting it, my logic is stuck in it and I can't get a result, I hope someone can give me a light on how to do this.

Update 2020-12-23 18:57 UTC

In response to comment by @GustavoMello

If $code merely dictates the sequence of $response , and $response is non-unique (ie, $response['code'] has duplicates):

$result = [];
$curResp = $response;

foreach ($code as $code2) {
    $newResp = [];  // Store the non-matching response data ...

    foreach ($curResp as $data) {
        if ($code2 === $data['code']) {
            $result[] = $data['rgb1'];
        } else {
            $newResp[] = $data;
        }
    }

    $curResp = $newResp;  // ... iterate over it on the next $code loop to reduce scope on the inner loop
}

var_dump($result);  // ['0500C2', 'A3A4A4', 'A3A4A4', 'C9C6BD', 'C9C6BD', 'C9C6BD']

However, the requirement that $code be non-unique as well, makes it too arbitrary, and requires the question have an example where $code and $response both have duplicates, and what the result should look like.


Original

If I understand correctly what you want, if $code is:

$code = [
    'BA',
    'SV',
];

Then the result should be:

[
    '0500C2',
    'A3A4A4',
];

Assuming $response['code'] is unique, we can reindex $code and $response by code, and call array_intersect_key() to filter $response :

$code = [
    'WA',
    'SI',
];
$code = array_flip($code);  // ['BA' => 0, 'BB' => 1, 'BK' => 2, ...]
$response = array_column($response, 'rgb1', 'code');  // ['BA' => '0500C2', ..., 'SV' => 'A3A4A4', ...]
$filtered = array_key_intersect($response, $code);  // ['WA' => '0500C2', 'SI' => 'A3A4A4']
$filtered = array_values($filtered);  // ['0500C2', 'A3A4A4']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM