duanpiyao2734 2016-07-25 02:53
浏览 37
已采纳

PHP数组合并递归问题

I'm getting an issue when trying to use array_merge_recursive in a foreach. What I'm trying to do is scrape a website then put all of the data into a multidimensional array. Here is my code:

Original Array

$apiarray = array(
"response" => array(
    "badges" => array(
        array()
    )
)
);

Array to be merged

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);
}

Output of printing array

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
                    )

            )

    )

)

Why does the first array in the multidimensional array return with

 [0] => Array //Why does this data appear?
                    (
                    ) 

There is only meant to be 4 array elements appearing since there is 4 results that will come from the foreach

  • 写回答

2条回答 默认 最新

  • dongyin2390 2016-07-25 03:19
    关注

    The empty first result comes from your definition:

    $apiarray = array(
        "response" => array(
            "badges" => array(
                array() // <- remove this guy
            )
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化