简体   繁体   English

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

[英]PHP - reading through multiple object(stdClass) in array

I have a variable, $result, that hold the following:我有一个变量 $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) "
"

If I var_dump the variable, "hello world" is rendered (because of document.write) in bold...among other elements in the array.如果我 var_dump 变量,“hello world”以粗体呈现(因为 document.write)......在数组中的其他元素中。

I want to only var dump content1 in the array.我只想在数组中 var dump content1。

I know I should be using -> but how do I do this when there are multiple object(stdClass).我知道我应该使用->但是当有多个对象(stdClass)时我该怎么做。

I only want the 1st record in the array.我只想要数组中的第一条记录。

I tried echo $result[0]->response[count];我试过echo $result[0]->response[count]; just to see if I could pull out the count value but it wasn't giving anything before trying to echo content1.只是想看看我是否可以取出计数值,但在尝试回显 content1 之前它没有给出任何内容。 How do I do this?我该怎么做呢?

can you please write the whole code, that seams to be a MYSQL Object right?你能写完整的代码吗,接缝是一个 MYSQL 对象,对吗?

You can read this: php stdClass to array您可以阅读: php stdClass to array

Or try to convert.或者尝试转换。

Converting an array/stdClass -> stdClass转换数组/stdClass -> stdClass

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

Or convert to an array $array = json_decode(json_encode($Result), true);或者转换为数组 $array = json_decode(json_encode($Result), true);

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

Hope this helps.希望这可以帮助。

Ohh just iterate over the object to find out what it prints. Ohh 只需遍历对象以找出它打印的内容。 If it prints all in one foreach loop then you can use this to index other elements.如果它在一个 foreach 循环中打印所有内容,那么您可以使用它来索引其他元素。

Or Try this:或者试试这个:

echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );

// test the extract
print_r( $websiteId_0 );

The proper syntax is:正确的语法是:

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

so所以

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

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

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