简体   繁体   English

回显json对象$ stdClass对象数组

[英]echo json object $stdClass Object array

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 我想使用foreach循环打印sku,名称,价格的值

Since im 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! 但我想在foreach中打印值,请帮忙!

Items is a property of the main object, and in itself is an array. Items是主对象的属性,而它本身是一个数组。 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

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
foreach($data['items'] as $item) {

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM