douou1891 2013-02-20 22:41
浏览 28
已采纳

将数组推入另一个数组

I have a site develop in php and I have a function where I want to create an array inside other array. My query are this (i'm using cakephp but in this case is only a problem of array don't tell me to use contain or something like that from cakephp because is a large query and I need to construct my query and my array in this mode)

My array $product_ingredient contain some value.

foreach ($product_ingredient as $key) {         
                $ingredient_level1 = $this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
                WHERE ingredients_ingredients.ingredient_id = :ingredient_id
                AND  ingredients_ingredients.product_id = :id 
                AND  ingredients_ingredients.version_id = :version_id 
                AND  ingredients_ingredients.level = 1', array('id' => $id, 'ingredient_id' => $key['products_ingredients']['ingredient_id'], 'version_id' => $key['products_ingredients']['version_id']));


                foreach($ingredient_level1 as $key2){
                    $ingredient_level2 = array($this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
                    WHERE ingredients_ingredients.ingredient_id = :ingredient_id
                    AND  ingredients_ingredients.product_id = :id 
                    AND  ingredients_ingredients.version_id = :version_id 
                    AND  ingredients_ingredients.level = 2', array('id' => $id, 'ingredient_id' => $key2['ingredients_ingredients']['ingredient2_id'], 'version_id' => $key2['ingredients_ingredients']['version_id'])));
                    array_push($ingredient_level1, $ingredient_level2);
                }
                array_push($ingredient_ingredient, $ingredient_level1);
            }

The result is that:

array(
    (int) 0 => array(
        (int) 0 => array(
            'ingredients_ingredients' => array(
                'id' => '34',
                'level' => '1'
            )
        ),
        (int) 1 => array(
            (int) 0 => array(
                (int) 0 => array(
                    'ingredients_ingredients' => array(
                        'id' => '35',
                        'level' => '2'
                    )
                )
            )
        )
    )

But I would like this result

array(
    (int) 0 => array(
        (int) 0 => array(
            'ingredients_ingredients' => array(
                'id' => '34',
                'level' => '1',
                array(
                    'ingredients_ingredients' => array(
                        'id' => '35',
                        'level' => '2'
                    )
            )
        )
    )

How can I solve?

  • 写回答

1条回答 默认 最新

  • douguluan5102 2013-03-30 02:16
    关注

    Replace

    array_push($ingredient_ingredient, $ingredient_level1);
    

    by

    array_push(
        $ingredient_ingredient[0][0]['ingredients_ingredients'],
        $ingredient_level1
    );
    

    This pushes the array to the appropriate cascade level. However it is very likely that this is the most efficient way, if you're unable to modify SQL or array structure elsewhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答