从Array中检索多个数据

时间:2014-03-12 11:39:28

标签: php arrays object

几个月前我问过这个问题,但当时我只需要提取一个会员级别,现在我需要提取每个级别的名称,这里是数组:

Array
(
  [success] => 1
  [member] => Array
  (
    [0] => Array
    (
      [Levels] => Array
      (
        [1391447566] => stdClass Object
        (
          [Level_ID] => 1391447566
          [Name] => Team Membership
          [Cancelled] => 
          [CancelDate] => 
          [Pending] => 
          [UnConfirmed] => 
          [Expired] => 
          ExpiryDate] => 1397266537
          [SequentialCancelled] => 
          [Active] => 1
          [Status] => Array
          (
            [0] => Active
          )
          [Timestamp] => 1394588137
          [TxnID] => WL-1-1391447566
        )

        [1391540448] => stdClass Object
        (
          [Level_ID] => 1391540448
          [Name] => Gold Training Membership
          [Cancelled] => 
          [CancelDate] => 
          [Pending] => 
          [UnConfirmed] => 
          [Expired] => 
          [ExpiryDate] => 1396642789
          [SequentialCancelled] => 
          [Active] => 1
          [Status] => Array
          (
            [0] => Active
          )

          [Timestamp] => 1393967989
          [TxnID] => WL-1-1391540448
        )

        [1391540567] => stdClass Object
        (
          [Level_ID] => 1391540567
          [Name] => Platinum Training Membership
          [Cancelled] => 
          [CancelDate] => 
          [Pending] => 
          [UnConfirmed] => 
          [Expired] => 
          [ExpiryDate] => 1397302237
          [SequentialCancelled] => 
          [Active] => 1
          [Status] => Array
          (
            [0] => Active
          )

          [Timestamp] => 1394623837
          [TxnID] => WL-1-1391540567
        )

      )

      [PayPerPosts] => Array
      (
      )

    )

  )

  [supported_verbs] => Array
  (
    [0] => GET
    [1] => PUT
    [2] => DELETE
  )

)

我需要的是在每次具有不同数字的对象内拉出Level of Level,并将其添加到我需要保存在DB中的数组中。一个解决方案,只是为了提取信息,等等:

foreach ($response["member"][0]["Levels"] AS $level_key => $level_val) {
$level_name = $level_key->Name;
    echo 'Name: '.$level_name;
}

但它没有拉动任何东西,只是回响“名字”3次。

这是我以前只使用一个名字的代码:

$levels = $response["member"][0]["Levels"];
$firstLevel = array_shift(array_values($levels));
$membership = $firstLevel->Name;

谢谢!

1 个答案:

答案 0 :(得分:5)

试试:

$level_name = $level_val->Name;

$level_key是一个带有级别ID(?)的整数值。 $level_val是包含数据的关卡对象。