简体   繁体   English

PHP从数组中获取多个数据

[英]PHP get multiple data from an array

I'm trying to insert data from the LinkedIn API into a MySql database. 我正在尝试将LinkedIn API中的数据插入MySql数据库中。

My PHP code is getting all the values from the Array which is not in the second level. 我的PHP代码正在从不在第二级的数组中获取所有值。 I don't know how to explain this, but I'll include the Array structure below: 我不知道如何解释这一点,但是我将在下面包含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] => ########
        )

)

My PHP code is as follows: 我的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);

What i need is to get all the "Skills" and seperate them by comma ",". 我需要获取所有“技能”并以逗号“,”将它们分开。

If my code worked I would have got the following value as $skills = "Start-ups, Business-strategy, ...." 如果我的代码有效,我将获得以下值:$ skills =“初创企业,业务战略....”

Ideas? 想法?

You can use nested Foreach loop for this.Like 您可以为此使用嵌套的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM