'How to get value from array with given variable?

I have this option lists that is generated dynamically :

 $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>';
     }

Output

<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>

Each option contains a different varian_color_id that points to a specific product_id in a json array like this :

$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)"
                },

In this scenario the Heather White option with variant_color_id=5522 points to the sync_product id 118421780 in the json array.

If the only thing that is known is the variant_color_id from the options, how can I use it to get the sync_product id from the array? Is it possible to reverse engineer?

I'm thinking to check if the digits are part of the array is a good start :

if (in_array($variant_color_id, $products)) {
     echo $variant_color_id .' is sync_product_id '.$x;
}


Solution 1:[1]

There's no way to use in_array for deep search like that.
I think this trivial loop will do the trick.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stopi