簡體   English   中英

根據傳遞第一個數組中的一個值的另一個數組在數組中搜索值

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

我嘗試了幾種方法來完成這項工作,但我做不到,我希望這里有人能幫助我。

api 返回名為 CODE 的數組中的所有值。 我想根據該數組的代碼獲取所有 rgb1。

例如

這是數組$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 )

這是 api 響應 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 )

這是我的代碼

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);
  }
}

但它沒有用,它返回的值與我在代碼數組中搜索的值不同。 我以其他方式嘗試過,但我沒有得到它,我的邏輯卡在里面,我得不到結果,我希望有人能告訴我如何做到這一點。

更新時間 2020-12-23 18:57 UTC

回應@GustavoMello 的評論

如果$code僅僅規定了$response的順序,並且$response是非唯一的(即$response['code']有重復項):

$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']

但是, $code也不是唯一的要求使其過於武斷,並且要求問題有一個示例,其中$code$response都有重復項,以及結果應該是什么樣子。


原來的

如果我正確理解你想要什么,如果$code是:

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

那么結果應該是:

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

假設$response['code']是唯一的,我們可以通過代碼重新索引$code$response ,並調用array_intersect_key()來過濾$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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM