Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include an array in another array

Was wondering how to add the values of one array into another to save me typing the values of one array over and over:

$array_main = array(
    '[1]' => '1',
    '[2]' => '2',
    '[3]' => '3',
    '[4]' => '4'
);

$array_1 = array( $array_main, '[5]' => '5' );

This deduces:

$array_1 = array(
    array(
        '[1]' => '1',
        '[2]' => '2',
        '[3]' => '3',
        '[4]' => '4'
    ),
    '[5]' => '5'
);

But I wanted:

$array_1 = array(
    '[1]' => '1',
    '[2]' => '2',
    '[3]' => '3',
    '[4]' => '4',
    '[5]' => '5'
);

So is there anything that can turn an array into a string? I've tried implode and array_shift but I need the whole array() not just the values..

like image 436
markb Avatar asked Feb 23 '26 23:02

markb


1 Answers

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

http://php.net/manual/en/function.array-merge.php

like image 186
Kld Avatar answered Feb 26 '26 14:02

Kld