LoginSignup
2
0

More than 5 years have passed since last update.

PHP オブジェクトのプロパティにエスケープが必要な文字が入っててハマった。

Posted at

jsonをデコードしたオブジェクトのプロパティに
/やらが入っていて少しハマったので。。

相変わらずド初心者なので詳しいことは偉い人に聞いてください!

今回要素を取得したいオブジェクト

twitterAPIのアクセス制限情報のjsonから抜粋。
プロパティにエスケープが必要な文字列が含まれてます。

hoge.php

# $object の中身が…
object(stdClass)#4 (2) {
  ["/followers/ids"]=>
  object(stdClass)#5 (2) {
    ["limit"]=>
    int(15)
    ["remaining"]=>
    int(15)
  }
}

解決策

当然そのまま

hoge.php
$object->/followers/ids;

ってやると「突然"/"出てきたんだけど!?」って怒られます。
条件反射的にエスケープしても…

hoge.php
$object->\/followers\/ids;

「突然"\"出てきたんだけど!?」って怒られます(残当)。

hoge.php
$object->{"/followers/ids"};

で許してもらえます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0