我如何获得数组名称并回显它?

时间:2015-01-27 05:36:59

标签: php

我的php

if($user){

    try{
        $user_profile = $facebook->api('/me');
        $params = array('next' => 'website' );
        $logout =$facebook->getLogoutUrl($params);
        $_SESSION['User']=$user_profile;
        foreach ($user_profile['education'] as $education) { // <-ERROR HERE
            if ($education['type'] == "College") {
                $college = $education;
                break;
            }
        }

        if(empty($college)) {
    echo "College information was not found!";
} else {
    // var_dump($college);
    $_SESSION['type'] = $college;
    print_r($college);
}

页面返回

Array ( [school] => Array ( [id] => 156029264583245 [name] => Universiti Tunku Abdul Rahman ) [type] => College )

我尝试了很多方法来解决它,但他们都没有工作。他们都返回php错误,我的意图是回应学校的名称并将其存储到$ SESSION。但我甚至无法得到学校名称的回声。我该怎么办才能得到阵列数据?谢谢

也尝试了这个

 $college = json_decode($education, true);
         echo $college[1]["name"];

不能正常工作。

3 个答案:

答案 0 :(得分:1)

因此,PHP数组是基于零的,以获得您需要的第一个对象[0]而不是[1]

 echo $college[0]["name"];

或者你去名字:

echo $college['school']['name'];

答案 1 :(得分:0)

Array ( [school] => Array ( [id] => 156029264583245 [name] => Universiti Tunku Abdul Rahman ) [type] => College )

如果这已经是你得到的那么你可以简单地,

echo $college['school']['name'];

答案 2 :(得分:-1)

试试这个会起作用:

echo $college['school']['name'];
它会回应学校的名字。