Pregunta

I have a form which submits a variable, let's call it $_POST['sku'].

This form also submits some variables to an external webapp (Shopify), which returns an array, as such:

['product']['variants'], which has up to 27 variants i.e.

['product']['variants'][1]
['product']['variants'][2]
['product']['variants'][3] ...
['product']['variants'][27]

Each variant has several keys, such as:

['product']['variants'][0]['id']
['product']['variants'][0]['sku'];

That is to say, variant 0 has both a unique id and sku, as does variant 1, variant 2, etc.

I would like to use php to extract the value from the 'id' key where the 'sku' value is equal to the $_POST['sku'] value submitted from my form. For example,

if $_POST['sku'] = 222

and ['product']['variants'][3]['sku'] = 222

then I would like to extract ['product']['variants'][3]['id'].

I'm pretty lost here -- any ideas?

¿Fue útil?

Solución

So basically:

foreach($variable['product']['variants'] as $i=>$variant) {
   if($variant['sku'] == $_POST['sku']) {
      // found it in $variable['product']['variants'][$i] so variant $i
      break;
   }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top