У меня есть сайт, который разрабатывается в php, и у меня есть функция, где я хочу создать массив внутри другого массива. Мой запрос: (я использую cakephp, но в этом случае проблема только в массиве не говорит мне, что я должен использовать содержащийся или что-то вроде этого из cakephp, потому что это большой запрос, и мне нужно построить мой запрос и мой массив в этом режиме)Вставить массив внутри другого массива
Мой массив $ product_ingredient содержит некоторое значение.
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);
}
В результате:
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'
)
)
)
)
)
Но я хотел бы этот результат
array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '34',
'level' => '1',
array(
'ingredients_ingredients' => array(
'id' => '35',
'level' => '2'
)
)
)
)
Как я могу решить?
Вы ищете 'array_merge' (http://php.net/manual/en/function.array-merge.php)? Я не совсем уверен, что вы собираетесь использовать с массивами образцов, поскольку массив, который вы пытаетесь создать, недействителен (несколько ключей с тем же именем и т. Д.). –
array_merge добавить только массив. Я хочу, чтобы массив внутри другого массива @Dave –
В вашем желаемом виде кода, я считаю, что у вас есть дополнительный 'массив (' строка справа после «изменено ...» и должен заменить его запятой. Правильно? – DACrosby