简体   繁体   中英

getting value from json array

My json array result is:

{

    "status":"ok",
    "message":"Success v1.",
    "remainingquota":"200",
    "results":[
        {
            "timestamp":"2014-01-24 23:26:55",
            "id":"426709995816624128",
            "category":"sports",
            "username":"KiranKS",
            "displayname":"KiranKS",
            "profileimage":"http://pbs.twimg.com/profile_images/415170263957778432/eS9f8sz0_normal.jpeg",
            "tweet_text":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
            "title":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
            "city":"mumbai",
            "rt_count":"43",
            "sentiment":"0",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":260,
                        "w":339
                    }
                }
            ],
            "updated":"18:12:10"
        },
        {
            "timestamp":"2014-01-24 15:16:48",
            "id":"426635550083534849",
            "category":"sports",
            "username":"LFCIndia",
            "displayname":"LFCIndia",
            "profileimage":"http://pbs.twimg.com/profile_images/3005882644/6a76508e39ce2105e88bea69b24e576c_normal.jpeg",
            "tweet_text":"Happy Birthday @luis16suarez! #JustCantGetEnough #LFCIndia http://t.co/Xv4f3qlVUL",
            "title":"Happy Birthday @luis16suarez! #JustCantGetEnough #LFCIndia http://t.co/Xv4f3qlVUL",
            "city":"new delhi",
            "rt_count":"13",
            "sentiment":"0",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Beu2kqYCQAAr6iu.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":340,
                        "w":340
                    }
                }
            ],
            "updated":"23:07:59"
        },
        {
            "timestamp":"2014-01-25 03:49:09",
            "id":"426879169273028608",
            "category":"sports",
            "username":"AltCricket",
            "displayname":"Alternative Cricket",
            "profileimage":"http://pbs.twimg.com/profile_images/378800000739216153/e99558c729acccbd6d0d4950b7257044_normal.jpeg",
            "tweet_text":"Ishant Sharma has been dropped for India. I guess he didn't see it coming: http://t.co/mXvtHGLWxl",
            "title":"Ishant Sharma has been dropped for India. I guess he didn't see it coming: http://t.co/mXvtHGLWxl",
            "city":"india",
            "rt_count":"10",
            "sentiment":"0",
            "powertweet":"1",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/BeyTL8hCIAAqX4P.jpg",
                    "type":"img"
                }
            ],
            "updated":"06:59:56"
        },
        {
            "timestamp":"2014-01-24 19:24:47",
            "id":"426628493196029952",
            "category":"sports",
            "username":"Sou_Reddevil",
            "displayname":"Sou_Reddevil",
            "profileimage":"http://pbs.twimg.com/profile_images/423135765434880000/vtOHs2PD_normal.jpeg",
            "tweet_text":"Still a better negotiator than Ed Woodward #MUFC @aditya_reds http://t.co/uVNQhwCHxq",
            "title":"Still a better negotiator than Ed Woodward #MUFC @aditya_reds http://t.co/uVNQhwCHxq",
            "city":"hyderabad",
            "rt_count":"9",
            "sentiment":"40",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Beuv49RCIAEtbnF.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":511,
                        "w":339
                    }
                }
            ],
            "updated":"23:36:02"
        },
        {
            "timestamp":"2014-01-24 14:03:46",
            "id":"426695551275393025",
            "category":"sports",
            "username":"DDNewsLive",
            "displayname":"DDNewsLive",
            "profileimage":"http://pbs.twimg.com/profile_images/3295146457/bcc4f0e184c107b1c8f4db9acb8d542b_normal.jpeg",
            "tweet_text":"All in good spirit: #RafaelNadal, #RogerFederer hug after Nadal won men's singles semi-final match at #AustralianOpen http://t.co/ThFiahlRl6",
            "title":"All in good spirit: #RafaelNadal, #RogerFederer hug after Nadal won men's singles semi-final match at #AustralianOpen http://t.co/ThFiahlRl6",
            "city":"new delhi",
            "rt_count":"8",
            "sentiment":"45",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/BevtBXhCQAIlxUj.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":273,
                        "w":340
                    }
                }
            ],
            "updated":"19:09:34"
        },
        {
            "timestamp":"2014-01-25 00:05:03",
            "id":"426785995208802305",
            "category":"sports",
            "username":"ScholesLegacy",
            "displayname":"ScholesLegacy",
            "profileimage":"http://pbs.twimg.com/profile_images/424796863066144768/gF95iWs-_normal.jpeg",
            "tweet_text":"Agent Rooney brilliant on the job. http://t.co/7gHxFDeanv",
            "title":"Agent Rooney brilliant on the job. http://t.co/7gHxFDeanv",
            "city":"mumbai",
            "rt_count":"7",
            "sentiment":"80",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Bew_eqJCAAAmJ5l.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":511,
                        "w":339
                    }
                }
            ],
            "updated":"13:10:10"
        },
        {
            "timestamp":"2014-01-24 18:32:40",
            "id":"426712202591354880",
            "category":"sports",
            "username":"DelhiDaredevils",
            "displayname":"DelhiDaredevils",
            "profileimage":"http://pbs.twimg.com/profile_images/412911860107595776/MYQ8boPd_normal.jpeg",
            "tweet_text":"#DD congratulates Women's Cricket Team Captain Mithali Raj! Her ton helped India win 3 match series against Sri Lanka http://t.co/kfBlMmfGLy",
            "title":"#DD congratulates Women's Cricket Team Captain Mithali Raj! Her ton helped India win 3 match series against Sri Lanka http://t.co/kfBlMmfGLy",
            "city":"india",
            "rt_count":"6",
            "sentiment":"80",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/BewB_L-CQAIW2MS.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":340,
                        "w":340
                    }
                }
            ],
            "updated":"17:54:15"
        },
        {
            "timestamp":"2014-01-24 17:41:39",
            "id":"426767605664186368",
            "category":"sports",
            "username":"ShivamYagnik",
            "displayname":"ShivamYagnik",
            "profileimage":"http://pbs.twimg.com/profile_images/378800000762607882/34903620999e5a5b26a502ffd0deb608_normal.jpeg",
            "tweet_text":""@Unitedology: Juan Mata's friend and hairdresser confirms he has joined United on Instagram... http://t.co/OTrZTnN4b5"@utkarshvmehta",
            "title":""@Unitedology: Juan Mata's friend and hairdresser confirms he has joined United on Instagram... http://t.co/OTrZTnN4b5"@utkarshvmehta",
            "city":"mumbai",
            "rt_count":"6",
            "sentiment":"0",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Bews-uYIgAAiwPC.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":605,
                        "w":340
                    }
                }
            ],
            "updated":"14:23:15"
        },
        {
            "timestamp":"2014-01-24 15:19:11",
            "id":"426706363931037696",
            "category":"sports",
            "username":"karthik_CFC",
            "displayname":"karthik_CFC",
            "profileimage":"http://pbs.twimg.com/profile_images/422725285780275200/2YEBc6C7_normal.jpeg",
            "tweet_text":"Someone from Fb posted it.,just wanted to share with loyal Chelsea fans,not Mata fan boys.,#CFC http://t.co/dB6Mz3s2vs",
            "title":"Someone from Fb posted it.,just wanted to share with loyal Chelsea fans,not Mata fan boys.,#CFC http://t.co/dB6Mz3s2vs",
            "city":"bangalore",
            "rt_count":"4",
            "sentiment":"47",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Bev3DunIEAA0s_p.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":604,
                        "w":340
                    }
                }
            ],
            "updated":"18:26:36"
        },
        {
            "timestamp":"2014-01-24 09:12:08",
            "id":"426639734446645250",
            "category":"sports",
            "username":"Gotham3",
            "displayname":"Gotham3",
            "profileimage":"http://pbs.twimg.com/profile_images/1839631891/320183_10150876927320227_848920226_21226871_267543349_n_normal.jpg",
            "tweet_text":"Indian cricketer Sreesanth getting weighed with bananas on scale!! http://t.co/Hjgwe2jO5b",
            "title":"Indian cricketer Sreesanth getting weighed with bananas on scale!! http://t.co/Hjgwe2jO5b",
            "city":"new delhi",
            "rt_count":"3",
            "sentiment":"0",
            "powertweet":"0",
            "entity":[
                {
                    "url":"http://pbs.twimg.com/media/Beu5cSgCMAEhMMz.jpg",
                    "type":"img",
                    "info":{
                        "type":"img",
                        "h":228,
                        "w":339
                    }
                }
            ],
            "updated":"22:51:22"
        }
    ]

}

To get the value:

$code = json_decode($json,true);
foreach($code as $arr) {
    foreach($arr as $k=>$v) {
       $rt_count = $arr[$k]['rt_count']; 
    }
}

now to get the entity->url

$img_url = $arr[$k]['entity']['url']; 

is this correct way?

First of all Your json string is not correct. Error in ending braces . I have corrected this. Check below:-

$json = '{"status":"ok",
"message":"Success v1.",
"remainingquota":"200",
"results":[

    {
        "timestamp":"2014-01-24 23:26:55",
        "id":"426709995816624128",
        "category":"sports",
        "username":"KiranKS",
        "displayname":"KiranKS",
        "profileimage":"http://pbs.twimg.com/profile_images/415170263957778432/eS9f8sz0_normal.jpeg",
        "tweet_text":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
        "title":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
        "city":"mumbai",
        "rt_count":"43",
        "sentiment":"0",
        "powertweet":"0",
        "entity":[
            {
                "url":"http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png",
                "type":"img",
                "info":{
                    "type":"img",
                    "h":260,
                    "w":339
                        }
            }
                ]
        }
        ]
}';


$code = json_decode($json,true);
echo "<pre>";print_r($code);echo "<pre>";

if You only want to get this " http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png " , then the below line will give You the perfect result:-

echo $code["results"][0]["entity"][0]["url"];

You can just do this:

$code = json_decode($json);
$img_url = $code->results[0]->entity[0]->url;

There is no need to loop through, unless you are pulling several entities and your json is just incomplete.

Try this. This would work.

foreach($code['results'] as $result){
  foreach($result['entity'] as $entity){
    $img_url = $entity['url'];
    echo $img_url . ' ';
  }
}

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