duanpiyao2734 2016-07-24 18: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-24 19:19
    关注

    The empty first result comes from your definition:

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部