PHP 数组合并递归问题
PHP array merge recursive issue
我在 foreach 中尝试使用 array_merge_recursive 时遇到问题。
我想做的是抓取一个网站,然后将所有数据放入一个多维数组中。这是我的代码:
原始数组
$apiarray = array(
"response" => array(
"badges" => array(
array()
)
)
);
待合并数组
foreach($recentbadges as $badge)
{
++$bcount;
$pbadge = $badge->find('div.profile_badges_badge', 0);
$pbadge = strip_tags($badge, '<div><br />');
preg_match('@data-community-tooltip="([^"]+)"@', $pbadge, $matchg);
$pbadge = array_pop($matchg);
$pbadge = array(
"response" => array(
"badges" => array(
array(
"badge_title" => "{$pbadge}"
)
)
)
);
$apiarray = array_merge_recursive($arrayfull, $pbadge);
}
输出数组
Array
(
[response] => Array
(
[badges] => Array
(
[0] => Array //Why does this data appear?
(
)
[1] => Array
(
[badge_title] => Heraldbr Level 2 Skullgirls Badge
)
[2] => Array
(
[badge_title] => Gold Cubebr Level 5 GooCubelets Badge
)
[3] => Array
(
[badge_title] => Four Color Acesbr Level 5 Why So Evil 2: Dystopia Badge
)
[4] => Array
(
[badge_title] => Teleport Controllerbr Level 3 Heaven Island Life Badge
)
)
)
)
为什么多维数组中第一个数组return和
[0] => Array //Why does this data appear?
(
)
只有 4 个数组元素出现,因为有 4 个结果将来自 foreach
从原始数组中的徽章中删除 array()
,例如:
$apiarray = array(
"response" => array(
"badges" => array()
)
);
空的第一个结果来自你的定义:
$apiarray = array(
"response" => array(
"badges" => array(
array() // <- remove this guy
)
)
);
我在 foreach 中尝试使用 array_merge_recursive 时遇到问题。 我想做的是抓取一个网站,然后将所有数据放入一个多维数组中。这是我的代码:
原始数组
$apiarray = array(
"response" => array(
"badges" => array(
array()
)
)
);
待合并数组
foreach($recentbadges as $badge)
{
++$bcount;
$pbadge = $badge->find('div.profile_badges_badge', 0);
$pbadge = strip_tags($badge, '<div><br />');
preg_match('@data-community-tooltip="([^"]+)"@', $pbadge, $matchg);
$pbadge = array_pop($matchg);
$pbadge = array(
"response" => array(
"badges" => array(
array(
"badge_title" => "{$pbadge}"
)
)
)
);
$apiarray = array_merge_recursive($arrayfull, $pbadge);
}
输出数组
Array
(
[response] => Array
(
[badges] => Array
(
[0] => Array //Why does this data appear?
(
)
[1] => Array
(
[badge_title] => Heraldbr Level 2 Skullgirls Badge
)
[2] => Array
(
[badge_title] => Gold Cubebr Level 5 GooCubelets Badge
)
[3] => Array
(
[badge_title] => Four Color Acesbr Level 5 Why So Evil 2: Dystopia Badge
)
[4] => Array
(
[badge_title] => Teleport Controllerbr Level 3 Heaven Island Life Badge
)
)
)
)
为什么多维数组中第一个数组return和
[0] => Array //Why does this data appear?
(
)
只有 4 个数组元素出现,因为有 4 个结果将来自 foreach
从原始数组中的徽章中删除 array()
,例如:
$apiarray = array(
"response" => array(
"badges" => array()
)
);
空的第一个结果来自你的定义:
$apiarray = array(
"response" => array(
"badges" => array(
array() // <- remove this guy
)
)
);