0

I want to fetch a single object from the array. From the array I need to fetch the nodes object

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' 
4
  • 1
    Show us what you have tried. Commented Nov 25, 2015 at 8:12
  • check my updated post Commented Nov 25, 2015 at 8:15
  • 1
    But after you json_encode it its not any more either PHP array or object.. If your idea is to convert it from object to array you can use json_decode(json_encode($object), TRUE); Commented Nov 25, 2015 at 8:15
  • 1
    $content_value is already in json format so you have to first decode it (use function json_decode)and it will make a simple php array and now you can fetch the keys of that array. Commented Nov 25, 2015 at 8:40

2 Answers 2

1

just iterate through the stdClass. There is no need to json encode the $request variable.

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

        var_dump($node->name);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I want to list $list->nodes
then remove the inner foreach and use var_dump($list->nodes);
if I encoded while inserting I got [{"name":"dsf","commentuser":"dsfdsf dsf dsfdsf","id":51,"date":"2015-11-25T09:07:52.314Z","displayDate":"Wed Nov 25 2015 14:37:52 GMT+0530 (India Standard Time)51","Like":0,"Unlike":0,"rating":0,"reportAbuse":0},{"name":"dsf","commentuser":"dsfdsf dsf dsfdsf","id":35,"date":"2015-11-25T09:07:52.382Z","displayDate":"Wed Nov 25 2015 14:37:52 GMT+0530 (India Standard Time)35","Like":0,"Unlike":0,"rating":0,"reportAbuse":0}]
I want to display only {"name":"dsf","commentuser":"dsfdsf dsf dsfdsf","id":51,"date":"2015-11-25T09:07:52.314Z","displayDate":"Wed Nov 25 2015 14:37:52 GMT+0530 (India Standard Time)51","Like":0,"Unlike":0,"rating":0,"reportAbuse":0},{"name":"dsf","commentu‌​ser":"dsfdsf dsf dsfdsf","id":35,"date":"2015-11-25T09:07:52.382Z","displayDate":"Wed Nov 25 2015 14:37:52 GMT+0530 (India Standard Time)35","Like":0,"Unlike":0,"rating":0,"reportAbuse":0}
I get exact answer in $node= json_encode(($list->nodes[0]));
1

just do $php_array = json_decode(json_encode($your_object_array)); now you can get the key of php_array like->$php_array['alblist'][0]['nodes'];

1 Comment

I caught the error Cannot use object of type stdClass as array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.