【问题标题】:Search for value within an array according to another array that passes one of the values ​within the first array根据传递第一个数组中的一个值的另一个数组在数组中搜索值
【发布时间】:2021-04-01 17:24:05
【问题描述】:

我尝试了几种方法来完成这项工作,但我做不到,我希望这里有人可以帮助我。

该api返回称为CODE的数组内的所有值。我想根据那个数组的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);
  }
}

但是没有用,它返回的值与我在代码数组中搜索的不同。我尝试了其他方式,但我没有得到它,我的逻辑卡在其中,我无法得到结果,我希望有人能告诉我如何做到这一点。

【问题讨论】:

    标签: php arrays loops search filter


    【解决方案1】:

    2020-12-23 18:57 UTC 更新

    回复comment by @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']
    

    【讨论】:

    • 对不起,我问的时候不是很清楚,但是$response['code']不是唯一的,第一个数组的CODE也不是,我想要的是根据['code'来组织颜色]。例如:如果$code = ['WA', 'SI']$response = ['SI' =&gt; '0500C2' , 'WA' = &gt; 'A3A4A4']; 我需要包含颜色和代码的数组根据第一个的索引对齐:$code = ['WA', 'SI'];$newResponse = ['WA' =&gt; 'A3A4A4' , 'SI' = &gt; '0500C2'];
    • @GustavoMello 嗯...所以基本上$code 只是规定了$response 的顺序/顺序(通过$response['code']). And since $response['code']` 不是唯一的,都具有相同的代码将组合在一起,但按原来的方式排序。但是,使用非唯一的 $code 几乎/没有意义。使用重复的 $code$response 更新您的帖子,以及您期望的结果。
    • @GustavoMello 根据您目前的反馈更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2021-10-20
    • 2017-07-02
    • 2016-05-26
    • 2022-01-01
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多