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

0

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Damien the code worked ..Thank you so much for fast reply
0

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

Comments

0
foreach($data['items'] as $item) {

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.