PHP从数组中获取多个数据

时间:2015-02-23 11:36:38

标签: php mysql arrays

我正在尝试将LinkedIn API中的数据插入到MySql数据库中。

我的PHP代码从Array获取的所有值都不在第二级。我不知道如何解释这个,但我将在下面包含Array结构:

Array
(
    [person] => Array
        (
            [id] => ########
            [first-name] => ########
            [last-name] => ########
            [languages] => Array

            [skills] => Array
                (
                    [skill] => Array
                        (
                            [id] => 1
                            [skill] => Array
                                (
                                    [name] => Start-ups
                                )

                            [0] => Array
                                (
                                    [id] => 2
                                    [skill] => Array
                                        (
                                            [name] => Business Strategy
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 3
                                    [skill] => Array
                                        (
                                            [name] => Marketing Strategy
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 12
                                    [skill] => Array
                                        (
                                            [name] => Management
                                        )

                                )



                        )

                )

            [email-address] => ########
            [phone-numbers] => ########
            [im-accounts] => ########
            [twitter-accounts] => ########
            [headline] => ########
            [picture-url] => ########
            [public-profile-url] => ########
        )

)

我的PHP代码如下:

/* SKILLS */
    // An array to hold it temporarily
    $skills_arr = array();
    foreach ($array->skills->skill as $t) {
      // Cast it to a string to get the text value back
      $skills_arr[] = (string)$t;
    }

    // Implode the array to a string
    $skills = implode(', ', $skills_arr);

我需要的是获得所有“技能”并用逗号“,”分隔它们。

如果我的代码有效,我会得到以下价值:$ skills =“初创企业,商业策略,......”

想法?

1 个答案:

答案 0 :(得分:0)

你可以使用嵌套的Foreach循环。喜欢

$skills_arr = array();
foreach($array['person']['skills']['skill'] as $val)
{
     foreach($val['skill'] as $res)
     {
         $skills_arr[] = $res['name'];
     }
}
echo $skills = implode(', ', $skills_arr);