
私は PHP アプリケーションを開発しており、次の配列があります。print_r($array)
Array (
[0] => stdClass Object (
[can_see_custom_stories] => 1
[name] => caitlinhamm
[display] =>
[type] => 3
)
[1] => stdClass Object (
[can_see_custom_stories] => 1
[name] => vladhq
[display] =>
[type] => 0
)
[2] => stdClass Object ( [can_see_custom_stories] => 1 [name] => interne [display] => Vlad [type] => 0 )
[3] => stdClass Object ( [can_see_custom_stories] => 1 [name] => frankiedoyle30 [display] => [type] => 0 )
[4] => stdClass Object ( [can_see_custom_stories] => 1 [name] => ayyeitskenzie03 [display] => [type] => 0 )
[5] => stdClass Object ( [can_see_custom_stories] => 1 [name] => laynedawwg [display] => Layne [type] => 0 )
[6] => stdClass Object ( [can_see_custom_stories] => 1 [name] => sarah.murphy225 [display] => [type] => 1 )
[7] => stdClass Object ( [can_see_custom_stories] => 1 [name] => rath1 [display] => Rathwaan [type] => 0 )
[8] => stdClass Object ( [can_see_custom_stories] => 1 [name] => brittanyyo [display] => Brit [type] => 0 )
[9] => stdClass Object ( [can_see_custom_stories] => 1 [name] => teamsnapchat [display] => Snapchat [type] => 0 )
[10] => stdClass Object ( [can_see_custom_stories] => 1 [name] => falconpunch1209 [display] => Colin Parker [type] => 0 )
[11] => stdClass Object ( [can_see_custom_stories] => 1 [name] => shawn_bonds [display] => Shawn Bonds [type] => 0 )
[12] => stdClass Object ( [can_see_custom_stories] => 1 [name] => chrisknowles [display] => Chris [type] => 0 )
[13] => stdClass Object ( [can_see_custom_stories] => 1 [name] => pypereronipizza [display] => [type] => 0 )
[14] => stdClass Object ( [can_see_custom_stories] => 1 [name] => wannaknow123 [display] => [type] => 0 )
[15] => stdClass Object ( [can_see_custom_stories] => 1 [name] => zainthugmalik [display] => Zain [type] => 1 )
[16] => stdClass Object ( [can_see_custom_stories] => 1 [name] => quinlanjade [display] => Quin [type] => 1 )
)
何らかのステートメントなしで、なんとか簡単に通過させたいと思っていますforeach()
。
配列は です$snapchat->getFriends()
。次のようなことは可能でしょうか:
$user = 'shawn_bonds';
$display = $snapchat->getFriends()->name['shawn_bonds']->display;
元の名前に基づいて表示名を取得するにはどうすればよいですか?
ありがとう!
答え1
Vlad さん、配列のアクセスされた要素のインデックスを指定する必要があります。例:
$snapchat->getFriends()[1]->name;
インデックスを見つけるには、 を使用できますforeach
。 使用したくないとおっしゃっていることは承知していますが、配列がオブジェクトで構成されていることを考慮すると、これが適切な配列インデックスを見つける最善の方法であると私は信じています。
そうでなければ配列の要素にアクセスすることはできません。Stackoverflowに関する質問ほぼ同じことです。