我正在开发一个 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 上的问题几乎是同一件事。