【问题标题】:echo json object $stdClass Object arrayecho json 对象 $stdClass 对象数组
【发布时间】:2014-05-25 07:49:07
【问题描述】:
echo "<pre>"; print_r($data); echo "</pre>";

给出以下输出:

$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
)

我想使用 foreach 循环打印 sku 、名称、价格的值

由于我是新手,所以我首先开始打印单个值

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

但我想打印 foreach 中的值,请帮忙!

【问题讨论】:

    标签: php arrays stdclass


    【解决方案1】:

    Items 是主对象的一个​​属性,它本身就是一个数组。这就是你所追求的:

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

    如果你想在没有循环的情况下访问其中一个元素,你需要提供数组索引,例如:

    echo $data->items[0]->name
    

    【讨论】:

    • 感谢@Damien,代码正常工作..非常感谢您的快速回复
    【解决方案2】:

    最简单的方法是将对象转换为数组

    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
    

    【讨论】:

      【解决方案3】:
      foreach($data['items'] as $item) {
      
         echo $item['sku'].PHP_EOL
         echo $item['name'].PHP_EOL
         echo $item['price'].PHP_EOL;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 2013-01-13
        • 2023-04-04
        • 2015-12-13
        • 2019-05-22
        • 2012-05-28
        相关资源
        最近更新 更多