0

While printing values I am getting error Cannot use object of type MongoDB\BSON\ObjectId as array

Here is my print_r data

    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
                  
                )
 

I am trying to access [Source] from both array

Here is my code -

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

1 Answer 1

1

$data is an object. You need to use arrow syntax to access the properties.

foreach($data->newsSources as $item)
    echo $item->source;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.