douyue7408 2011-09-05 10:34
浏览 10
已采纳

如何在PHP中将数组缩进另一个数组

I have an array structure like the following:

enter image description here

I would like to have the elements keys, bi, media and offer under biz_id. Like this:

biz_id
 bi
 media
 offer
biz_id
...

My code is the following:

    foreach ($premiumContent as $targets=>$target)
{

    $finalResult[]["biz_id"] = $target->biz_id;

    foreach ($target->media as $media)
     {
        switch ($media->type_id)
        {
            case '1':
                $finalResult[]["info"] = $media->text;
            break;
            case '6':
                $finalResult[]["bi"] = $media->url;
            break; 
            case '4':
                $mediaGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break;
            case '3':
                $offerGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break; 
            default:
                echo '';
        }
    }
    $finalResult[]["media"] = $mediaGroup;
    $finalResult[]["offer"] = $offerGroup;
}

return $finalResult;

What can I do to group the keys underneath biz_id?

Thanks for the recommendations.

展开全部

  • 写回答

4条回答 默认 最新

  • dsxz84851 2011-09-05 10:46
    关注

    Simple: make the biz_id the key of the array:

    foreach ($premiumContent as $targets=>$target)
    {
    
        foreach ($target->media as $media)
         {
            switch ($media->type_id)
            {
                case '1':
                    $finalResult[$target->biz_id]["info"] = $media->text;
                break;
                case '6':
                    $finalResult[$target->biz_id]["bi"] = $media->url;
                break; 
                case '4':
                    $mediaGroup[] = array('link' => $media->url, 'descript' => $media->text);
                break;
                case '3':
                    $offerGroup[] = array('link' => $media->url, 'descript' => $media->text);
                break; 
                default:
                    echo '';
            }
        }
        $finalResult[$target->biz_id]["media"] = $mediaGroup;
        $finalResult[$target->biz_id]["offer"] = $offerGroup;
    }
    
    return $finalResult;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部