gpt4 book ai didi

PHP从数组中获取多个数据

转载 作者:行者123 更新时间:2023-11-30 22:49:04 25 4
gpt4 key购买 nike

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

我的 PHP 代码从不在第二层的数组中获取所有值。我不知道如何解释这一点,但我将在下面包含数组结构:

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 = "Start-ups, Business-strategy, ..."

想法?

最佳答案

您可以为此使用嵌套的 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);

关于PHP从数组中获取多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28672889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com