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

时间:2017-01-06 23:16:42

标签: php arrays

我有一个变量$ 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('\r\n\r\n');" 
["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变量,&#34; hello world&#34;被渲染(因为document.write)粗体...以及数组中的其他元素。

我只想在数组中使用var dump content1。

我知道我应该使用->但是如果有多个对象(stdClass),我该怎么做呢。

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

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

2 个答案:

答案 0 :(得分:1)

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

您可以阅读:php stdClass to array

或尝试转换。

转换数组/ stdClass - &gt; stdClass的

$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 );

答案 1 :(得分:1)

正确的语法是:

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

所以

echo $result[0]->response->content1
相关问题