从数组中获取单个对象

时间:2015-11-25 08:11:53

标签: php arrays

我想从数组中获取单个对象。从数组我需要获取节点对象

print_t($request); returns the the following json
200 - stdClass Object
(
[pagename] => album_comment
[albid] => n4l5h
[alblist] => Array
    (
        [0] => stdClass Object
            (
                [name] => 
                [commentuser] => 
                [id] => 0
                [Totalcount] => 1
                [nodes] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => hjkjk
                                [commentuser] => hjkjk hj hkhj
                                [id] => 56
                                [nodes] => Array
                                    (
                                    )

                                [date] => 2015-11-25T08:18:34.111Z
                                [displayDate] => Wed Nov 25 2015 13:48:34 GMT+0530 (India Standard Time)56
                                [Like] => 0
                                [Unlike] => 0
                                [rating] => 0
                                [reportAbuse] => 0
                            )

                    )

                [getcomment] => hjkjk hj hkhj
                [username] => hjkjk
            )

    )

  )

$content_value = json_encode($request->alblist);

$content_value = [{"name":"",
                   "commentuser":"",
                   "id":0,
                   "nodes":[{"name":"bm",
                             "commentuser":"bmnbnmbn",
                             "id":79,
                             "date":"2015-11-  25T08:03:07.765Z"
                           }]
                 }]
   $content_value1 = json_encode($request);   
  $content_value1={"pagename":"album_comment","albid":"n4l5h","alblist":[{"name":"","commentuser":"","id":0,"Totalcount":1,"nodes":[{"name":"hgj","commentuser":"ghjhgj","id":52,"nodes":[],"date":"2015-11-25T08:15:57.710Z","displayDate":"Wed Nov 25 2015 13:45:57 GMT+0530 (India Standard Time)52","Like":0,"Unlike":0,"rating":0,"reportAbuse":0}],"getcomment":"ghjhgj","username":"hgj"}]}  

 I tried to fetch as
  $var=$content_value['nodes']; //shows the error Illegal string offset 'nodes' 

2 个答案:

答案 0 :(得分:1)

只做$php_array = json_decode(json_encode($your_object_array)); 现在你可以获得php_array的关键字 - > $php_array['alblist'][0]['nodes'];

答案 1 :(得分:1)

只是遍历stdClass。没有必要对$request变量进行json编码。

foreach($request->alblist as $list) {
    foreach($list->nodes as $node) {

        var_dump($node->name);
    }
}