【问题标题】:Want to iterate through this array of objects i am bringing back from facebook Graph Api - Oauth2.0想要遍历我从 facebook Graph Api - Oauth2.0 带回来的这个对象数组
【发布时间】:2013-02-02 00:13:47
【问题描述】:
    $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 ) ) 

我已经尝试使用嵌套的 foreach 循环对其进行迭代..

     foreach($education as $educationFirstArray){
     foreach($educationFirstArray as $educationSecondArray){
        foreach($educationSecondArray as $key=>$value){
         echo "<strong>".$key."</strong> -> ".$value."<br/>";
        }
     }
   }  

输出:

 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

任何有关使该数组高于迭代的帮助都将非常感激,在此先感谢..

【问题讨论】:

  • 您需要提供正确的打印输出和代码的每一部分。
  • @qeremy:我已经把它作为一个问题呈现出来,并添加了代码的 for-each 部分和输出,我真的在与它作斗争。只是希望你现在会调查这个问题。干杯。!

标签: php facebook-graph-api multidimensional-array oauth-2.0 iteration


【解决方案1】:

首先,我必须说这个数据结构非常复杂。数组或对象乱七八糟。反正..

我认为您需要一个简单地循环所有数据结构的递归函数;

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 名称-> Vardhman Shiksha Mandir ID -> 136328419721520 名称 -> 2009 类型 -> 高中 ID -> 115934828416686 名称->贾米亚·哈姆德拉德 ID -> 187549374618419 名称 -> B.Tech 计算机科学 类型 -> 学院 ID -> 133233016699548 名称 -> 哈姆达德大学 类型 -> 学院 ID -> 196267900399898 姓名 -> 第二年 编号 -> 553736429 名称 -> 艾哈迈德·阿卜杜勒·扎赫拉 编号 -> 553736429 名称 -> 艾哈迈德·阿卜杜勒·扎赫拉 ID -> 104008189635968 名称 -> 杰米亚·哈姆达德 ID -> 138879996141011 名称 -> 2013 类型 -> 研究生院

这里是你的数据结构的模拟,它给出了你自己的相同打印;

$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'
    ) 
);

【讨论】:

  • 是的,这是一个很好的答案!完全解决了我的问题,非常感谢您帮助我。我必须说这是我在 Stackoverflow 上的第一个问题,很高兴有你@qeremy。谢谢兄弟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2020-11-26
  • 2019-04-03
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多