简体   繁体   English

如何解析PHP stdObject

[英]How To Parse a PHP stdObject

Hello There I have the following code: 您好,我有以下代码:

stdClass Object ( 
  [num_rows] => 3 [row] => Array ( 
     [product_id] => 2439 [author_id] => 39 [author_attribute_id] => 0 
   ) [rows] => Array ( 
     [0] => Array ( 
       [product_id] => 2439 [author_id] =>  39 [author_attribute_id] => 0 ) 
     [1] => Array ( 
       [product_id] => 2439 [author_id] => 156 [author_attribute_id] => 0 ) 
     [2] => Array ( 
       [product_id] => 2439 [author_id] => 684 [author_attribute_id] => 0 )
    ) 
 ) 

now I'd like to extract the author_id values. 现在,我想提取author_id值。 How do I do this? 我该怎么做呢?

You might want to access properties do it like that: 您可能想要访问属性 ,就像这样:

$obj->property

Or if you don't often use object you might find this function useful: 或者,如果您不经常使用对象,则可能会发现此功能很有用:

function objectToArray($obj)
{
    if (is_object($obj))
        $obj = get_object_vars($obj);
    if (is_array($obj))
        return array_map(__CLASS__.'::'.__FUNCTION__, $obj);
    else
        return $obj;
}

It converts an Object to an Array. 它将对象转换为数组。

例如: $obj->$row->author_id

first of all thanks for all your answers Solved my problem with the following code 首先感谢您的所有回答,使用以下代码解决了我的问题

 for($i=0;$i<obj->num_rows;$i++)    {
    $array = obj->rows[i];
    echo $array['author_id'];
    }

Well thanks again for posting your answers......and sorry for late reply...... 很好,再次感谢您发布答案...并且对您的答复表示歉意...

$object->row->author_id $对象 - >按行> AUTHOR_ID

foreach ($object->rows as $row) {
    echo $row->author_id;
}

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

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