想知道我怎么能在php中提取json的索引部分。 一直困扰着我多年。谢谢!
$ch = curl_init('https://domain.com/blah/blah');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$promo = curl_exec($ch);
curl_close($ch);
$promo = json_decode($promo, true);
{"something":"blahhhh","name":"bob!","id":"7","select":[{"Index":1,"code":"a1","name":"hello","description":"more text"},{"Index":2,"code":"a2","name":"bye","description":"test.."},{"Index":3,"code":"a3","name":"ayeee","description":"Morning!"},{"Index":4,"code":"a4","name":"Cheese!","description":"Yummy!"},{"Index":5,"code":"a5","name":"Water","description":"why is it cloudy? :( "}],"chant":"Free the ducks!","joined":"2015-01-01T16:49:05.000+0000","cool":false}
答案 0 :(得分:0)
当json_decode的第二个参数设置为true时,该函数返回一个非常接近JSON结构的关联数组。
从那里你要做的就是将JSON路径放回到关联的php数组中以访问想要的值!
foreach($promo['select'] as $select){
echo $select['Index'].' ';
}
示例:http://codepad.org/OMg7WTr0
如果你想提取其他值并且你没有弄清楚路径只是做var_dump($promo);
它会让你清楚地看到要使用的数组路径