PHPでJSONのObject(stdClass)を扱う
詰まったのでメモ。
例えばこんな感じのJSONをPHPでデコードしたいとする。
object(stdClass)#1 (2) { ["check"]=> int(1) ["code"]=> object(stdClass)#2 (3) { ["title"]=> string(3) "ABC" ["description"]=> string(8) "testtest" ["items"]=> array(1) { [0]=> object(stdClass)#3 (2) { ["url"]=> string(31) "http://www.proto-star.com/" ["title"]=> string(0) "" } } } }
この時、[“title”]の値を取り出したければ、
$obj = json_decode($json); echo $obj->code->title;
という感じに書けばよいが、 [“items”] の [“url”] を取り出そうとするとつまづく。