【问题标题】:How to echo values from this array? [duplicate]如何从这个数组中回显值? [复制]
【发布时间】:2017-08-30 05:16:25
【问题描述】:

谁能帮我从以下数组中回显 [file] 值和 [label] 值:

Array
(
    [0] => stdClass Object
        (
            [label] => 360p
            [type] => video/mp4
            [file] => /uploads/myVideo.mp4
            [res] => 360p
        )

    [1] => stdClass Object
        (
            [label] => 720p
            [type] => video/mp4
            [file] => /uploads/myVideo.mp4
            [res] => 720p
        )

    [2] => stdClass Object
        (
            [label] => 480p
            [type] => video/mp4
            [file] => /uploads/myVideo.mp4
            [res] => 480p
        )

)

【问题讨论】:

  • 您尝试过什么先自己解决这个问题?
  • foreach($data as $results): echo ''.$results['file'].''; echo ''.$results['label'].''; endforeach;
  • 你应该在你的问题中“乍一看”。 ^ 并用您尝试过的内容编辑您的问题stackoverflow.com/posts/43216819/edit@AbeliaZahfarina
  • 对不起,我是新来的……你能编辑我的问题吗? ^_^

标签: php arrays


【解决方案1】:

您可以foreach 循环遍历所有项目并在数组中调用,如:

foreach($yourArray as $item) {
    echo $item->label;
    echo $item->file;
}

或者您可以使用json_decode 将您的对象转换为数组。

$result = json_decode($data, true);

【讨论】:

  • 结果是致命错误:不能在第 31 行的 D:\xampp\htdocs\13.php 中使用 stdClass 类型的对象作为数组
  • @AbeliaZahfarina 抱歉,我看不到您的数据类型是对象。请参阅我的更新答案。
【解决方案2】:

您可以像这样访问stdClass 中的元素:

foreach ($test as $v) {
    echo $v->file;
    echo $v->label;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2012-05-22
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多