-2

This JSON snippet is part of a WooCommerce order, extracted from the APIv2 get_order() function. The [meta] holds product variation data chosen by the customer.

[meta] => Array ( [0] => stdClass Object ( [key] => pa_size [label] => Size [value] => Large ) [1] => stdClass Object ( [key] => pa_color [label] => Choose Color [value] => Black )

How do I search the stdClass object for a particular [key] and return its corresponding [value]?

D. Simpson
  • 1,882
  • 17
  • 32

2 Answers2

1
<?php
   $meta = array();   //Suppose that your array is $meta

   $your_key="pa_size"; // if you are trying to search for "pa_size"
   $your_value=""; // will retrun value "Large"

   foreach( $meta as $obj )
   {
        if( $obj->key == $your_key )
        {
            $your_value = $obj->value;
            break;
        }
   }

   echo $your_value; // will retrun value "Large"
?>
Prakash
  • 171
  • 7
0

This should do it:

foreach ($rows as $object) {
{
    $size  = $object->pa_size;
    $color = $object->pa_color;
    // more code
}
jeromegamez
  • 3,348
  • 1
  • 23
  • 36