jsonãphpã§parseããéã«ã³ãã³ãå«ã¾ãã¦ãæã®å¯¾å¿ã¡ã¢
æããã«æ¬¡ãã¯ã¾ããããªã®ã§ãã¡ã¢
ãããããã¨
rssãjsonå½¢å¼ã§è¿ãã¦ããããµã¤ãããã£ãã®ã§ããããphpã§parseãããã¨æã£ãã
code
$rss_file = file_get_contents($rss_url); $rss_data_obj = json_decode($rss_file);
json_decodeããå ´åãstdClassã§è¿å´ãããã
object(stdClass)#22 (1) { ["feed"]=> object(stdClass)#23 (8) { ["author"]=> ...
stdClassãªã®ã§ã以ä¸ã®ããã«å¤ãåãã°ããã®ã ãã
$rss_data_obj->feed->author;
å°ã£ãã®ã¯ãããããã¿ã¼ã³
["entry"]=> object(stdClass)#27 (12) { ["im:name"]=> object(stdClass)#28 (1) { ...
['im:name'] ã¨ããååã ã¨ã->im:name ã¨ããããã«ãªãã¸ã§ã¯ãã¨ãã¦å¼ã³åºããªãã
ãããããªãã®ã§arrayã«ãã£ã¹ããã
åè: Object→Arrayにキャストする ~多次元配列編~ | Suinasia
private function obj2arr($obj) { if ( !is_object($obj) ) return $obj; $arr = (array) $obj; foreach ( $arr as &$a ) { $a = $this->obj2arr($a); } return $arr; }
ããã§ã ãããarrayã«ãã£ã¹ãã§ãããã ããå
ã
é
åãå«ã¾ããã¨ãé
å以ä¸ãè¦ã¦ãããªãäºã«æ°ã¥ããã
["icon"]=> array(1) { ["label"]=> string(35) "http://itunes.apple.com/favicon.ico" } ["link"]=> array(2) { [0]=> object(stdClass)#59 (1) { ["attributes"]=> ...
"icon" ã¯ãã¾ãarrayã«ãã£ã¹ãã§ãã¦ãããã©ã"link"ã¯å ã arrayãªã®ã§ã[0]åç®ã®è¦ç´ 以ä¸ããã£ã¹ãããã¦ãªãã
ãããããªãã®ã§ä»¥ä¸ã®ããã«ããã
private function obj2arr($obj) { if ( !is_object($obj) && $this->is_hash($obj) ) return $obj; $arr = (array) $obj; foreach ( $arr as &$a ) { $a = $this->obj2arr($a); } return $arr; } private function is_hash($array) { if (!is_array($array)) { return true; } list($k) = each($array); return $k !== 0; }