简体   繁体   English

如何在数组内的 stdClass 内的字符串上执行 foreach?

[英]How to do a foreach on a string inside a stdClass inside a array?

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 :但我需要一个 foreach NR 之后:

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.我之前问过一个类似的问题,该问题因重复而关闭( 如何使用 PHP 从 JSON 中提取数据? It is saved in the DB as varchar and the vardump says it is a string.它作为 varchar 保存在数据库中,vardump 表示它是一个字符串。

A solution came through the comments by Nigel Ren (@nigelren):一个解决方案来自 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>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM