Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

Push array inside another array

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?

Upvotes: 1

Views: 481

Answers (1)

dialogik
dialogik

Reputation: 9542

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.

Upvotes: 3

Related Questions