2014-05-25 3 views
0
echo "<pre>"; print_r($data); echo "</pre>"; 

는 다음과 같은 출력을 제공합니다 :

내가 처음 시작이에 메신저 새로운 때문에 foreach 루프

를 사용하여 SKU, 이름, 가격의 가치를 인쇄 할

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

단일 값 인쇄

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

그러나 foreach 값을 인쇄하고 싶습니다.

답변

4

항목의 주요 객체의 속성이며, 그 자체로 배열입니다 배열 개체를 변환합니다. 이것은 당신이 계신 : 코드 @Damien

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

감사 : 당신은 루프없이 그 요소 중 하나에 액세스하려면

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

, 당신은 예를 들어, 배열 인덱스를 제공해야 빨리 대답 해 주셔서 대단히 감사합니다. – 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; 

}