PHP - 读取数组中的多个对象(stdClass)



我有一个变量$result,它保存以下内容:

object(stdClass) #1 (1) { ["response"]= > object(stdClass) #2 (6) { 
["status"]= > string(2) "OK" 
["count"] => int(1)
["flash_click_variable"] => NULL
["no_iframes"] => bool(false)
["content1"] => string(765)
"document.write('rnrn');" 
["content2"] => string(711)
"<strong>hello world</strong>
" ["
file_name "]=> NULL ["
track_clicks "]=> bool(true) ["
audit_status "]=> string(7) "
audited " ["
macros "]=> string(12) "
ADV_ID, CP_ID " ["
profile_id "]=> NULL ["
audit_feedback "]=> string(0) "
"

如果我var_dump变量,"hello world"将以粗体呈现(因为 document.write)......在数组中的其他元素中。

我只想在数组中转储 var content1。

我知道我应该使用->但是当有多个对象(stdClass)时我该怎么做。

我只想要数组中的第一条记录。

我尝试echo $result[0]->response[count];只是为了看看我是否可以提取计数值,但在尝试回显内容1之前它没有给出任何东西。我该怎么做?

正确的语法是:

echo $result[0]->response->count

所以

echo $result[0]->response->content1

你能写出整个代码吗,接缝是MYSQL对象吗?

你可以读到这个: php stdClass to array

或者尝试转换。

转换数组/标准类 -> 标准类

$stdClass = json_decode(json_encode($Result));

或转换为数组$array = json_decode(json_encode($Result), true);

echo $Result["response"]["content1"];

希望这有帮助。

哦,只需迭代对象以找出它打印的内容。如果它在一个 foreach 循环中打印所有内容,则可以使用它来索引其他元素。

或者试试这个:

echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );
// test the extract
print_r( $websiteId_0 );

最新更新