【问题标题】:PHP Loop through array type stdClass ErrorPHP循环遍历数组类型stdClass错误
【发布时间】:2019-03-27 01:31:24
【问题描述】:

我知道这里有人问过这个问题:Loop through an array php 我正在为 ProductName 和 ProductId 寻找一个非常相似的解决方案,但之前的答案没有帮助。

Array ( [0] => stdClass Object ( 
[Id] => A048FDBF-67FF-4D28-8F53-7D41DCAC45A9 
[ProductDetail] => Array 
    ( 
        [0] => stdClass Object 
            ( 
                [ProductId] => A5C43424-FFD3-4097-80D4-01E04F759115 
                [ProductName] => Influencer Track 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T08:00:00 
                [EndTime] => 2019-10-08T18:00:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [1] => stdClass Object 
            ( 
                [ProductId] => 163AB381-1C3D-43AE-911D-0729982B8C5C 
                [ProductName] => BS 6:1 How to deal with the new normal using Sales & Operations Planning and Demand Driven MRP 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => With the inclusion of Sales and Operations Planning and DDMRP, IFS Applications 10 offers a highly competent set of planning capabilities. Join us in this session to discover how IFS planning strategies can be combined to drive process improvements beyond the expected. 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-09T13:00:00 
                [EndTime] => 2019-10-09T13:45:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [2] => stdClass Object 
            ( 
                [ProductId] => 0E1F44BA-54AA-48AC-B54D-0802CCCD3A5A 
                [ProductName] => BS 1:5 Insights session 
                [ProductCode] => [ProductType] => Session 
                [ProductDescription] => [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T13:45:00 
                [EndTime] => 2019-10-08T14:30:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [3] => stdClass Object 
            ( 
                [ProductId] => DA447217-EA3F-4A14-AD83-0AD184E1D5C4 
                [ProductName] => BS 1:1 Are you ready for tomorrow’s Challengers in Manufacturing? 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => Trends like automation, legislation and sustainability present both challenges and opportunities. Our Global Industry Directors will discuss current and emerging trends in the Manufacturing industry and how our customers are addressing them. Join our session to find out how you can leverage the latest trends to disrupt your market. 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T13:45:00 [EndTime] => 2019-10-08T14:30:00 [Status] => Active [Capacity] => -1 
            ) 
        [4] => stdClass Object 
            ( 
                [ProductId] => 244A8FBB-F731-43B5-909A-0B3DA39DF75C 
                [ProductName] => Influencer Dinner, time tbc 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-07T19:30:00 
                [EndTime] => 2019-10-07T22:30:00 
                [Status] => Active 
                [Capacity] => -1 
            )
        )
    )
)

我正在尝试这个,但我收到错误无法使用 stdClass 类型的对象作为数组

for ($i = 0; $i < count($events); $i++) {
      echo $events[$i]['ProductId'];
      echo $events[$i]['ProductName'];
}

forloop 是执行此操作的最佳方法吗?我是否必须使用 json_decode 或其他方法来提取 stdClass 对象?

【问题讨论】:

  • 是父数组$events的名字
  • 是的,很抱歉,不清楚,print_r ($events) 列出了完整的数组。
  • 看起来 $events 是您的父数组,但它本身就是一个数组,因此在您的情况下 $events[0]->ProductDetail 是您要迭代的数组。

标签: php arrays


【解决方案1】:

试试这个,看看它是否有效

foreach($events as $event) {
    foreach($event->ProductDetail as $product) {
        echo $product->ProductId;
        echo $product->ProductName;
    }
}

【讨论】:

    【解决方案2】:

    要访问数组中的项目,请使用方括号$array['ProductId']。 但是要访问对象中的属性,您必须使用箭头符号$object-&gt;ProductId。 两者兼而有之。

    你需要做类似的事情

    echo $events[$i]->ProductId;
    

    从根目录访问让我们说第一个产品 id 你会这样做

    echo $root[0]->ProductDetail[0]->ProductId;
    

    要访问所有产品 ID,您可以执行以下操作

    $products = $root[0]->ProductDetail;
    for ($i = 0; $i < count($products); $i++) { 
        echo $products[$i]->ProductId; 
    }
    

    或者

    $products = $root[0]->ProductDetail;
    foreach($products as $object) {
        echo $object->ProductId;
    }
    

    两者都产生相同的结果,只需 2 种方法即可。还有其他方法。我想这些是最常见的,也是您尝试过的一种方式。我建议你使用你最了解的那个。

    如果你想迭代外部数组,那么你也需要一个循环。在这种情况下,您将有一个嵌套循环。

    在我的示例中,我假设您只遍历内部产品详细信息数组。

    【讨论】:

    • 这是有道理的,我会试试这个,看看效果如何,谢谢。
    猜你喜欢
    • 2014-06-18
    • 2015-10-24
    • 2014-01-31
    • 2011-05-23
    • 2012-11-14
    • 2011-04-25
    • 1970-01-01
    • 2023-01-28
    • 2019-07-10
    相关资源
    最近更新 更多