0

I have this code:

$results = $db->loadObjectList();
var_dump($results);

I get the following:

array(1) { 
    [0]=> object(stdClass)#595 (1) { 
        ["relationto"]=> string(26) "{"0":"3","1":"2","2":"55"}" 
    } 
}

But I need a foreach NR after :

I asked a similar question before and that question was closed for duplicate (How do I extract data from JSON with PHP?) I have tried all the suggestions on the page but none work and I think it is because it ain't json. It is saved in the DB as varchar and the vardump says it is a string.

halfer
  • 19,824
  • 17
  • 99
  • 186
purple11111
  • 709
  • 7
  • 20

1 Answers1

0

A solution came through the comments by Nigel Ren (@nigelren):

// Original code
$results = $db->loadObjectList();
var_dump($results);

// Print the results
print_r( json_decode($results[0]->relationto, true));

// Creating the foreach
$datas = json_decode($results[0]->relationto, true);
foreach($datas as $data) {
  echo "<br><br>" . $data . "<br><br>";
}
halfer
  • 19,824
  • 17
  • 99
  • 186
purple11111
  • 709
  • 7
  • 20