-3

this array

{"user": {"username": "pian___", "connected_fb_page": null, "follows": {"count": 392}, "requested_by_viewer": false, "followed_by": {"count": 3213}, "country_block": null, "has_requested_viewer": false, "external_url_linkshimmed": null, "follows_viewer": true, "profile_pic_url_hd": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s320x320/12935153_283927491939445_911290443_a.jpg", "profile_pic_url": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/12935153_283927491939445_911290443_a.jpg", "is_private": false, "full_name": "newbie", "media": {"count": 2, "page_info": {"has_previous_page": false, "start_cursor": "1233303150505827739", "end_cursor": "1215833196294643512", "has_next_page": false}, "nodes": [{"code": "BEdkq5aRR2b", "dimensions": {"width": 1080, "height": 828}, "comments_disabled": false, "owner": {"id": "1410694422"}, "comments": {"count": 149}, "caption": "Mati satu tumbuh seribu. Kartini mungkin sudah tiada, namun semangat juangnya tidak boleh padam begitu saja. Maju terus para Kartini muda. Selamat hari Kartini\n.\n~ 21'4'16 Hari Kartini .", "likes": {"count": 580}, "date": 1461241214, "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c126.0.828.828/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2.c", "is_video": false, "id": "1233303150505827739", "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2"}, {"code": "BDfgdmrRR84", "dimensions": {"width": 1080, "height": 810}, "comments_disabled": false, "owner": {"id": "1410694422"}, "comments": {"count": 18}, "caption": "Jogja 2K16 \ud83d\udc81\ud83d\udcaa #jogja #candiborobudur #candiprambanan #candi #yogyakarta #mencari #keberadaan #nevada :'v", "likes": {"count": 127}, "date": 1459158633, "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c135.0.810.810/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2.c", "is_video": false, "id": "1215833196294643512", "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2"}]}, "blocked_by_viewer": false, "has_blocked_viewer": false, "followed_by_viewer": false, "is_verified": false, "id": "1410694422", "biography": "\ud83d\udccdIndonesia", "external_url": null}}

i need result of 'code', like this:

BEdkq5aRR2b,BDfgdmrRR84

can u help?^_^

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 1
    So, have you tried `json_decode`? What do you see? – u_mulder Dec 19 '16 at 12:49
  • 3
    Welcome to SO. Please note that SO is not a code-writing service. Please show your attempts before asking a question. You may want to try `array_map` in addition to `foreach` to fetch the values into an array quickly. – SOFe Dec 19 '16 at 12:49
  • You might want to have a quick glance at how to create a [mcve] if you haven't already. This will assist us provide you with better answers. – Henders Dec 19 '16 at 12:50

2 Answers2

2

Let's see a beautified version of the JSON:

{
    "user": {
        "username": "pian___",
        "connected_fb_page": null,
        "follows": {
            "count": 392
        },
        "requested_by_viewer": false,
        "followed_by": {
            "count": 3213
        },
        "country_block": null,
        "has_requested_viewer": false,
        "external_url_linkshimmed": null,
        "follows_viewer": true,
        "profile_pic_url_hd": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s320x320/12935153_283927491939445_911290443_a.jpg",
        "profile_pic_url": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/12935153_283927491939445_911290443_a.jpg",
        "is_private": false,
        "full_name": "newbie",
        "media": {
            "count": 2,
            "page_info": {
                "has_previous_page": false,
                "start_cursor": "1233303150505827739",
                "end_cursor": "1215833196294643512",
                "has_next_page": false
            },
            "nodes": [
                {
                    "code": "BEdkq5aRR2b",
                    "dimensions": {
                        "width": 1080,
                        "height": 828
                    },
                    "comments_disabled": false,
                    "owner": {
                        "id": "1410694422"
                    },
                    "comments": {
                        "count": 149
                    },
                    "caption": "Mati satu tumbuh seribu. Kartini mungkin sudah tiada, namun semangat juangnya tidak boleh padam begitu saja. Maju terus para Kartini muda. Selamat hari Kartini\n.\n~ 21'4'16 Hari Kartini .",
                    "likes": {
                        "count": 580
                    },
                    "date": 1461241214,
                    "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c126.0.828.828/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2.c",
                    "is_video": false,
                    "id": "1233303150505827739",
                    "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2"
                },
                {
                    "code": "BDfgdmrRR84",
                    "dimensions": {
                        "width": 1080,
                        "height": 810
                    },
                    "comments_disabled": false,
                    "owner": {
                        "id": "1410694422"
                    },
                    "comments": {
                        "count": 18
                    },
                    "caption": "Jogja 2K16  #jogja #candiborobudur #candiprambanan #candi #yogyakarta #mencari #keberadaan #nevada :'v",
                    "likes": {
                        "count": 127
                    },
                    "date": 1459158633,
                    "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c135.0.810.810/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2.c",
                    "is_video": false,
                    "id": "1215833196294643512",
                    "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2"
                }
            ]
        },
        "blocked_by_viewer": false,
        "has_blocked_viewer": false,
        "followed_by_viewer": false,
        "is_verified": false,
        "id": "1410694422",
        "biography": "Indonesia",
        "external_url": null
    }
}

We can see that the nodes you are looking for are located at ->user->media->nodes, so we can use json_decode to parse the data, and use -> to go down the object nests to get an array of the nodes:

$nodes = json_decode($jsonData)->user->media->nodes;

Then for each node we want to collect code attribute, so we map the node array into a code array using array_map:

$codes = array_map(function($node){
    return $node->code;
}, $nodes);

Then we can use implode to join pieces in the array into a string:

$output = implode(",", $codes);
Community
  • 1
  • 1
SOFe
  • 7,867
  • 4
  • 33
  • 61
0
<?php

// your json string
$json = '{"user": {"username": "pian___", "connected_fb_page": null, "follows": {"count": 392}, "requested_by_viewer": false, "followed_by": {"count": 3213}, "country_block": null, "has_requested_viewer": false, "external_url_linkshimmed": null, "follows_viewer": true, "profile_pic_url_hd": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s320x320/12935153_283927491939445_911290443_a.jpg", "profile_pic_url": "https://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/12935153_283927491939445_911290443_a.jpg", "is_private": false, "full_name": "newbie", "media": {"count": 2, "page_info": {"has_previous_page": false, "start_cursor": "1233303150505827739", "end_cursor": "1215833196294643512", "has_next_page": false}, "nodes": [{"code": "BEdkq5aRR2b", "dimensions": {"width": 1080, "height": 828}, "comments_disabled": false, "owner": {"id": "1410694422"}, "comments": {"count": 149}, "caption": "Mati satu tumbuh seribu. Kartini mungkin sudah tiada, namun semangat juangnya tidak boleh padam begitu saja. Maju terus para Kartini muda. Selamat hari Kartini\n.\n~ 21\'4\'16 Hari Kartini .", "likes": {"count": 580}, "date": 1461241214, "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c126.0.828.828/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2.c", "is_video": false, "id": "1233303150505827739", "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14360137_321090301587067_4196521787967668224_n.jpg?ig_cache_key=MTIzMzMwMzE1MDUwNTgyNzczOQ%3D%3D.2"}, {"code": "BDfgdmrRR84", "dimensions": {"width": 1080, "height": 810}, "comments_disabled": false, "owner": {"id": "1410694422"}, "comments": {"count": 18}, "caption": "Jogja 2K16 \ud83d\udc81\ud83d\udcaa #jogja #candiborobudur #candiprambanan #candi #yogyakarta #mencari #keberadaan #nevada :\'v", "likes": {"count": 127}, "date": 1459158633, "thumbnail_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c135.0.810.810/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2.c", "is_video": false, "id": "1215833196294643512", "display_src": "https://scontent-sin6-1.cdninstagram.com/t51.2885-15/e35/14474382_1187975291263026_6316075023123611648_n.jpg?ig_cache_key=MTIxNTgzMzE5NjI5NDY0MzUxMg%3D%3D.2"}]}, "blocked_by_viewer": false, "has_blocked_viewer": false, "followed_by_viewer": false, "is_verified": false, "id": "1410694422", "biography": "\ud83d\udccdIndonesia", "external_url": null}}';


// 1) Decode your json string
$json_decoded = json_decode($json);
//var_dump($json_decoded);


// 2) Get the part you want, in your case, the nodes in user->media
$nodes = $json_decoded ->user->media->nodes;
//var_dump($nodes);

// Temporary var to retain the codes
$codes = array();

foreach($nodes as $node)
{

    $codes[]=$node->code;

}

//var_dump($codes);


// Your output
echo implode("," , $codes);

// Will print:  BEdkq5aRR2b,BDfgdmrRR84

Hope this helps you.