简体   繁体   中英

issue to get post index in foreach

i have problem to get post index from following code in php

stdClass Object ( 
    [blog] => stdClass Object ( 
        [ask] => 1 
        [ask_anon] => 1 
        [ask_page_title] => Ask me anything 
        [can_subscribe] => 
        [description] => 
        [is_adult] => 
        [is_nsfw] => 
        [name] => fashion 
        [posts] => 3068 
        [reply_conditions] => 3 
        [share_likes] => 
        [subscribed] => 
        [title] => Fashion 
        [total_posts] => 3068 
        [updated] => 1511888740 
        [url] => http://fashion.tumblr.com/ 
        [is_optout_ads] => 1 
    ) 
    [posts] => Array ( 
            [0] => stdClass Object ( 
                    [type] => photo 
                    [blog_name] => fashion 
                    [id] => 167980084706 
                    [post_url] => http://fashion.tumblr.com/post/167980084706/art-by-lucamaininipsychodiva 
                    [slug] => art-by-lucamaininipsychodiva 
                    [date] => 2017-11-28 17:05:40 GMT 
                    [timestamp] => 1511888740 
                    [state] => published 
                    [format] => html 
                    [reblog_key] => TQXtN3TU 
                    [tags] => Array ( 
                            [0] => luca mainini 
                            [1] => gif 
                            [2] => art 
                            [3] => fashion 
                            [4] => design 
                            [5] => fashionontumblr 
                    ) 
                    [short_url] => https://tmblr.co/ZSVahx2SSPAtY 
                    [summary] => Art by @lucamaininipsychodiva 
                    [is_blocks_post_format] => 
                    [recommended_source] => 
                    [recommended_color] => 
                    [note_count] => 75 
                    [source_url] => http://lucamaininipsychodiva.tumblr.com/post/120540766379/gif-collage-for-vulkan-magazine 
                    [source_title] => lucamaininipsychodiva

You can go for:-

$object->posts[0]->blog_name;

Or if you want to go with foreach():-

foreach($object->posts as $key=>$value){
 echo $key;
 echo $value->id;
}

Note:- in second-one $key is the indexes of sub-array like 0,1,2,3.. and $value is the sub-array itself.

Ok first things first, when you decode from json to array you need to use ,true So in step before this stdclass object use $array=json_decode($myArray,true); This will give you an array which is what you want. After that a multidimensional array is handled easily you just need to loop around it:

foreach ($array['posts'] as $row){
  echo $row['id'] //for example
}

If you still want to use stdclass object then you can still access it like that:

foreach ($result->posts as $row) {
    echo $row->id;
}

I always prefer to end up with arrays to handle but that's up to you. Those are my 2 solutions pick which fits best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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