1
    $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

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

4

1 回答 1

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'
    ) 
);
于 2013-02-17T15:37:00.590 回答