简体   繁体   中英

PHP get multiple data from an array

I'm trying to insert data from the LinkedIn API into a MySql database.

My PHP code is getting all the values from the Array which is not in the second level. I don't know how to explain this, but I'll include the Array structure below:

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:

/* 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, ...."

Ideas?

You can use nested Foreach loop for this.Like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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