【问题标题】:PHP: getting error Cannot use object of type MongoDB\BSON\ObjectId as array?PHP:出现错误不能使用 MongoDB\BSON\ObjectId 类型的对象作为数组?
【发布时间】:2020-09-09 13:35:25
【问题描述】:

打印值时出现错误无法使用类型的对象 MongoDB\BSON\ObjectId 作为数组

这是我的 print_r 数据

    stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5f55f95815a8508e2deac8dd
        )

    [title] => Consumers to Gain as Telcos Cut Broadband Rates
    [summary] => Consumers are set to benefit from a broadband  
    [newsSources] => Array
        (
            [0] => stdClass Object
                (
                    [title] => Consumers to Gain as Telcos Cut Broadband Rates
                    [articleId] => 003a0593-f0c3-11ea-98d1-1418779a6e32
                    [link] => https://myimpact.in/clipj_admin.php?id=95c14189533fa7c87a8ce6752cae40f5
                    [type] => print
                    [source] => The Economic Times - Kolkata
                     
                )

            [1] => stdClass Object
                (
                    [title] => Data Plans Coming Much Cheaper with Telcos at War, Again
                    [articleId] => 3c4052f6-f0ac-11ea-959b-0068ebdc2fd0
                    [link] => https://myimpact.in/clipj_admin.php?id=95126ad546a574dd8233c2b0365d5f01
                    [type] => print
                    [source] => The Economic Times - Mumbai
                  
                )
 

我正在尝试从两个数组访问 [Source]

这是我的代码 -

  foreach($cursor as $row){
                    //     echo"<pre>";
                  
                    //    print_r($row);
                  
                       foreach($row as $data) {
                        foreach($data['newsSources'][0] as $k) {
                              echo $k->source ;
                              
                        }
                 }
 }

【问题讨论】:

    标签: php arrays mongodb


    【解决方案1】:

    $data 是一个对象。您需要使用箭头语法来访问属性。

    foreach($data->newsSources as $item)
        echo $item->source;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多