2014-05-25 32 views
0
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中的值請幫助!

回答

4

項目是主要對象的屬性,本身就是一個數組。這就是你追求的:

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

如果要訪問這些元素的一個無環路,您需要提供數組的索引,例如:

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

感謝@Damien代碼工作..謝謝你的快速回復 – 55555nam

0

爲您簡單的方法是轉換對象數組

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

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

}