[英]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. api 返回名为 CODE 的数组中的所有值。 I want to get all rgb1 according to the CODE of that array.
我想根据该数组的代码获取所有 rgb1。
For example :例如:
This is array $code :这是数组$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:这是 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 )
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.
我以其他方式尝试过,但我没有得到它,我的逻辑卡在里面,我得不到结果,我希望有人能告诉我如何做到这一点。
In response to comment by @GustavoMello回应@GustavoMello 的评论
If $code
merely dictates the sequence of $response
, and $response
is non-unique (ie, $response['code']
has duplicates):如果
$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']
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.但是,
$code
也不是唯一的要求使其过于武断,并且要求问题有一个示例,其中$code
和$response
都有重复项,以及结果应该是什么样子。
If I understand correctly what you want, if $code
is:如果我正确理解你想要什么,如果
$code
是:
$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
:假设
$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.