【问题标题】:Looping through array contain 3 classes遍历数组包含 3 个类
【发布时间】:2010-12-09 14:01:12
【问题描述】:

我无法理解如何循环遍历 stdClasses。

打印数组给了我以下信息:

Array
(
    [piggyback] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1003
                    [entity_id] => 0
                    [redirect_url] => http://yahoo.com
                    [type] => Image
                )

        )

    [total_count] => 1
)
Array
(
    [piggyback] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1002
                    [entity_id] => 0
                    [redirect_url] => http://google.com
                    [type] => Image
                )

        )

    [total_count] => 1
)
Array
(
    [piggyback] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1001
                    [entity_id] => 0
                    [redirect_url] => http://bing.com
                    [type] => Image
                )

        )

    [total_count] => 1

我正在尝试使用以下内容循环并打印出一个值(id),但我什么也没得到。

foreach ($piggies_array as $key => $value) {
  echo $piggies_array[$key]['id'];
}

【问题讨论】:

  • 你能解释一下你是如何打印数组的吗?

标签: php arrays loops


【解决方案1】:
foreach ($piggies_array as $key => $value) {
if (is_array($value)){
  echo $value[0]->id;
 }
}

【讨论】:

  • 感谢您和所有人。完美。
【解决方案2】:

我认为你需要:

for ($i = 0; $i < count($piggies_array); $i++) {
    echo $piggies_array[$i]['piggyback'][0]->id;
}

...假设我们只能看到您的部分输出;)

【讨论】:

    【解决方案3】:

    尝试将其替换为echo $value-&gt;id;

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2016-08-19
      • 2021-08-03
      • 1970-01-01
      相关资源
      最近更新 更多