Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
echo "<pre>"; print_r($data); echo "</pre>";

Gives following output:

$stdClass Object
(
    [cartName] => AngularStore
    [clearCart] => 
    [checkoutParameters] => stdClass Object
        (
        )

    [items] => Array
        (
            [0] => stdClass Object
                (
                    [sku] => 01
                    [name] => Product 1
                    [price] => 600
                    [quantity] => 1
                    [stock] => 5
                    [scheme] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => offerAB
                                    [desc] => Description on the scheme
                                    [no] => 3
                                    [$$hashKey] => 01O
                                    [checked] => 1
                                )

                            [1] => stdClass Object
                                (
                                    [name] => offerXY
                                    [desc] => Description on the scheme
                                    [no] => 5
                                    [$$hashKey] => 01P
                                )

                            [2] => stdClass Object
                                (
                                    [name] => OfferPQ
                                    [desc] => Description on the scheme
                                    [no] => 2
                                    [$$hashKey] => 01Q
                                    [checked] => 1
                                )

                            [3] => stdClass Object
                                (
                                    [name] => OfferLM
                                    [desc] => Description on the scheme
                                    [no] => 4
                                    [$$hashKey] => 01R
                                )

                        )

                    [$$hashKey] => 05V
                )

            [1] => stdClass Object
                (
                    [sku] => 02
                    [name] => Product 2
                    [price] => 500
                    [quantity] => 1
                    [stock] => 400
                    [scheme] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => offerAB
                                    [desc] => Description on the scheme
                                    [no] => 6
                                    [$$hashKey] => 01W
                                )

                            [1] => stdClass Object
                                (
                                    [name] => offerXY
                                    [desc] => Description on the scheme
                                    [no] => 7
                                    [$$hashKey] => 01X
                                )

                            [2] => stdClass Object
                                (
                                    [name] => OfferPQ
                                    [desc] => Description on the scheme
                                    [no] => 3
                                    [$$hashKey] => 01Y
                                )

                            [3] => stdClass Object
                                (
                                    [name] => OfferLM
                                    [desc] => Description on the scheme
                                    [no] => 8
                                    [$$hashKey] => 01Z
                                )

                        )

                    [$$hashKey] => 05W
                )

        )

    [qty] => 3
)

I want to print value of sku , name, price using foreach loop

Since i m new to it i first started printing a single value

echo $data->items->arr[0]->sku;
Notice: Trying to get property of non-object  getting this error

but i want to print the values in foreach please help!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Items is a property of the main object, and in itself is an array. This is what you're after:

foreach($data->items as $d) {
   echo $d->name, '<br />', $d->sku, '<br />', $d->price;
}

If you want to access one of those element without a loop, you need to provide the array index, for example:

echo $data->items[0]->name
share|improve this answer
    
thanks @Damien the code worked ..Thank you so much for fast reply –  user3593884 May 25 at 11:37

the easy way for you is convert object to array

function array2object($array) {

    if (is_array($array)) {
        $obj = new StdClass();

        foreach ($array as $key => $val){
            $obj->$key = $val;
        }
    }
    else { $obj = $array; }

    return $obj;
}

function object2array($object) {
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    }
    else {
        $array = $object;
    }
    return $array;
}


// example:

$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four');

$obj = array2object($array);

print $obj->one; // output's "two"

$arr = object2array($obj);

print $arr['foo']; // output's bar
share|improve this answer
foreach($data['items'] as $item) {

   echo $item['sku'].PHP_EOL
   echo $item['name'].PHP_EOL
   echo $item['price'].PHP_EOL;

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.