Robert Kuhlig
Robert Kuhlig

Reputation: 11

Loop trough object in PHP

I read trought How to loop through objects in php But still don't get it to work

Has someone an idea? Here is my php

$Topic_OBJ = json_decode($this->api_local_get($url));
$Text_IDS = "";
    
foreach ($Topic_OBJ as $obj) {
    $Text_IDS .= $obj->Text->Text_id.",";
}

$Topic_OBJ looks like

{
        "TextContainer_id": 339131662,
        "Text": [
            {
                "Text_id": 794887707,
                "TextContainer_id": 339131662
            },
            {
                "Text_id": 794887711,
                "TextContainer_id": 339131662
            }
        ]
    }

But Text_IDS is only ",," but should be "794887707,794887711,"

Can someone help me please? thanks rob

Upvotes: 0

Views: 45

Answers (1)

Simon K
Simon K

Reputation: 1523

You should loop over the $Topic_OBJ->Text array instead like so...

<?php

$Topic_OBJ = json_decode($this->api_local_get($url));
$Text_IDS = "";
    
foreach ($Topic_OBJ->Text as $text) {
    $Text_IDS .= $text->Text_id.",";
}

Upvotes: 1

Related Questions