Php 想要遍历我从FacebookGraphAPI-Oauth2.0带回的这个对象数组吗
我尝试过用嵌套的foreach循环来迭代它Php 想要遍历我从FacebookGraphAPI-Oauth2.0带回的这个对象数组吗,php,facebook-graph-api,multidimensional-array,oauth-2.0,iteration,Php,Facebook Graph Api,Multidimensional Array,Oauth 2.0,Iteration,我尝试过用嵌套的foreach循环来迭代它 $education = $user->education; Array ( [0] => stdClass Object ( [school] => stdClass Object ( [id] => 112629628784911 [name] => Vardhman Shiksha Mandir ) [year] => stdClass Object ( [id] => 13632841
$education = $user->education;
Array ( [0] => stdClass Object ( [school] => stdClass Object ( [id] => 112629628784911 [name] => Vardhman Shiksha Mandir ) [year] => stdClass Object ( [id] => 136328419721520 [name] => 2009 ) [type] => High School ) [1] => stdClass Object ( [school] => stdClass Object ( [id] => 115934828416686 [name] => jamia hamdrad ) [concentration] => Array ( [0] => stdClass Object ( [id] => 187549374618419 [name] => B.Tech Computer Science ) ) [type] => College ) [2] => stdClass Object ( [school] => stdClass Object ( [id] => 133233016699548 [name] => Hamdard University ) [type] => College [classes] => Array ( [0] => stdClass Object ( [id] => 196267900399898 [name] => 2nd year [with] => Array ( [0] => stdClass Object ( [id] => 553736429 [name] => Ahmed Abdul Zahra ) ) [from] => stdClass Object ( [id] => 553736429 [name] => Ahmed Abdul Zahra ) ) ) ) [3] => stdClass Object ( [school] => stdClass Object ( [id] => 104008189635968 [name] => Jamia Hamdard ) [year] => stdClass Object ( [id] => 138879996141011 [name] => 2013 ) [type] => Graduate School ) )
提前感谢您,让这个数组在上面迭代的任何帮助都是非常值得的。首先,我必须说这个数据结构非常复杂。数组或对象处于混乱状态。无论如何 我认为您需要一个循环遍历所有数据结构的递归函数
id -> 112629628784911
name -> Vardhman Shiksha Mandir
id -> 136328419721520
name -> 2009
Warning: Invalid argument supplied for foreach() in index.php on line 73
id -> 115934828416686
name -> jamia hamdrad
Catchable fatal error: Object of class stdClass could not be converted to string in index.php on line 74
产出
function looper($input) {
foreach ($input as $key => $val) {
// control here
if (is_array($val) || is_object($val)) {
looper($val);
} else {
printf("%s -> %s\n", $key, $val);
}
}
}
// call
looper($education);
首先,我必须说这个数据结构非常复杂。数组或对象处于混乱状态。无论如何 我认为您需要一个循环遍历所有数据结构的递归函数
id -> 112629628784911
name -> Vardhman Shiksha Mandir
id -> 136328419721520
name -> 2009
Warning: Invalid argument supplied for foreach() in index.php on line 73
id -> 115934828416686
name -> jamia hamdrad
Catchable fatal error: Object of class stdClass could not be converted to string in index.php on line 74
产出
function looper($input) {
foreach ($input as $key => $val) {
// control here
if (is_array($val) || is_object($val)) {
looper($val);
} else {
printf("%s -> %s\n", $key, $val);
}
}
}
// call
looper($education);
您需要为代码的每一部分提供适当的打印输出和输出。@qeremy:我已经将其作为一个问题呈现出来,并为代码的每一部分和输出添加了相应的内容,我真的在与之斗争。希望你现在能研究一下这个问题。。干杯您需要为代码的每一部分提供适当的打印输出和输出。@qeremy:我已经将其作为一个问题呈现出来,并为代码的每一部分和输出添加了相应的内容,我真的在与之斗争。希望你现在能研究一下这个问题。。干杯是的,这是一个很好的答案!完全解决了我的问题,非常感谢你的帮助。我必须说这是我关于Stackoverflow的第一个问题,有你@qeremy真的很高兴。谢谢兄弟。是的,这是一个很好的答案!完全解决了我的问题,非常感谢你的帮助。我必须说这是我关于Stackoverflow的第一个问题,有你@qeremy真的很高兴。谢谢兄弟。
$education = array(
(object) array(
'school' => (object) array('id' => '112629628784911', 'name' => 'Vardhman Shiksha Mandir'),
'year' => (object) array('id' => '136328419721520', 'name' => '2009'),
'type' => 'High School'
),
(object) array(
'school' => (object) array('id' => '115934828416686', 'name' => 'jamia hamdrad'),
'concentration' => array(
(object) array('id' => '187549374618419', 'name' => 'B.Tech Computer Science')
),
'type' => 'College'
),
(object) array(
'school' => (object) array('id' => '133233016699548', 'name' => 'Hamdard University'),
'type' => 'College',
'classes' => array (
(object) array('id' => '196267900399898', 'name' => '2nd year',
'with' => array((object) array('id' => 553736429, 'name' => 'Ahmed Abdul Zahra')),
'from' => (object) array('id' => 553736429, 'name' => 'Ahmed Abdul Zahra')
)
)
),
(object) array(
'school' => (object) array('id' => '104008189635968', 'name' => 'Jamia Hamdard'),
'year' => (object) array('id' => '138879996141011', 'name' => 2013),
'type' => 'Graduate School'
)
);