我有此选项列表,它是动态生成的:
$prod_variants = json_decode($synresponsecolpr, true);
foreach ($prod_variants["result"]["variants"] as $variant_color) {
echo '<option variant_color_id="'.$variantid.'"
value="'.$colorno.'"
class="productli">'.$variant_color['color'].'</option>';
}
输出
<select>
<option value="" class="">Select Color</option>
<option variant_color_id="5522" class="productli">
Heather White
</option>
<option variant_color_id="5523" class="productli">
Indigo
</option>
<option variant_color_id="5524" class="productli">
Premium Heather
</option>
</select>
每个选项都包含一个不同的varian_color_id
,它指向这样的json数组中的特定product_id
:
$products = json_decode($synresponsepr, true);
{
"code": 200,
"result": {
"sync_product": {
"id": 118421780,
"external_id": "5d02d83a0c81e4",
"name": "Hooded Sweatshirt - Black Logo - Men Hoodie",
"variants": 29,
"synced": 29
},
"sync_variants": [
{
"id": 1335748157,
"external_id": "5d02d83a0c8345",
"sync_product_id": 118421780,
"name": "Hooded Sweatshirt - Black Logo - Men Hoodie - White / S",
"synced": true,
"variant_id": 5522,
"retail_price": "29.00",
"currency": "USD",
"product": {
"variant_id": 5522,
"product_id": 146,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/146/5522_1526560589.jpg",
"name": "Gildan 18500 Unisex Heavy Blend Hooded Sweatshirt (White / S)"
},
在这种情况下,带有Heather White
的{{1}}选项指向json数组中的variant_color_id=5522
sync_product
id
。
如果唯一已知的是选项中的 variant_color_id ,如何使用它从数组中获取 sync_product id ?可以进行逆向工程吗?
我正在考虑检查数字是否是数组的一部分是一个好的开始:
118421780
答案 0 :(得分:0)
无法使用in_array
这样的深度搜索。
我认为这个琐碎的循环可以解决问题。
foreach ($products["result"]["sync_variants"] as $sv) {
if ($sv['product']['variant_id'] == $variant_color_id) {
echo $variant_color_id . ' is sync_product_id ' . $products['result']['sync_product']['id'];
break;
}
}