doushun1870 2015-01-29 08:39
浏览 29
已采纳

将2个数组连接在一起代码改进

There are 2 arrays. One that consists of Categories and one of Products. Each product pertains to its specific category. I want to join each product to its right category (a category can have multiple products). Each product that 'finds' its category will go in this category's products array.

Here's my code:

for ($i = 0; $i < count($prods); $i++)
{
    for ($u = 0; $u < count($cats); $u++)
    {
        if ($prods[$i]['category_code'] === $cats[$u]['category_style_code'])
        {
            if ( !isset($cats[$u]['products']) )
            {
                $cats[$u]['products'] = array();
            }
            array_push($cats[$u]['products'], $prods[$i]);
        }
    }
}

It results in something like:

Array
(
    [0] => Array
        (
            [id] => 1
            [category_style_code] => GA
            [products] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [default_price] => 37.50
                            [category_code] => GA
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [default_price] => 15.00
                            [category_code] => GA
                        )
                )
        )
)

Let's say there are many categories and many products... How would you optimize this code (or do it differently maybe there are PHP functions which could be used) ?

Edit: I also want to make the resulting array indexes be the its category code.

  • 写回答

2条回答 默认 最新

  • drphfy1198 2015-01-29 08:52
    关注

    What you can do to make it a bit better is to:

    • Use foreach.

    • Use category by reference.

    This way the code will look like this:

    foreach ($products as $product) {
        foreach ($categories as &$category) {
            if (!isset($category['Products'])) {
                $category['Products'] = array();
            }
            if ($product['category_code'] === $category['category_style_code']) {
                $category['Products'][] = $product;
            }
        }
    }
    

    EDIT Having PHP > 5.5.0 you can use the following code to have the category_style_code column as key:

    $keys = array_column($categories, 'category_style_code');
    $categories = array_combine($keys, $categories);
    

    This will combine an array that uses $categories as values and $keys as keys, where $keys we picked up all values in category_style_code column.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题