这个数组
{"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}}
我需要'代码'的结果,如下所示:
BEdkq5aRR2b,BDfgdmrRR84
你帮忙吗?^ _ ^
答案 0 :(得分:2)
让我们看看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
}
}
我们可以看到您要查找的节点位于->user->media->nodes
,因此我们可以使用json_decode
来解析数据,并使用->
向下移动对象嵌套得到一个节点数组:
$nodes = json_decode($jsonData)->user->media->nodes;
然后,对于我们想要收集code
属性的每个节点,我们使用array_map
将节点数组映射到代码数组中:
$codes = array_map(function($node){
return $node->code;
}, $nodes);
然后我们可以使用implode
将数组中的片段连接成一个字符串:
$output = implode(",", $codes);
答案 1 :(得分: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
希望这会对你有所帮助。