Pradip Garchar
Pradip Garchar

Reputation: 159

combine multidimensional array value into one array

I have two different array like-

$csvdata[]   = array("a", "b", "c");
$apendthis[] = array("d", "e", "f");

$result = array_combine($csvdata,$apendthis);
print_r($result);

It give result like this:-

Array ( 
    [Array] => Array ( 
        [0] => d 
        [1] => e 
        [2] => f 
    ) 
)

but I would like output :-

Array ( 
    [Array] => Array ( 
        [0] => a 
        [1] => b 
        [2] => c 
        [3] => d 
        [4] => e 
        [5] => f 
    ) 
)

Upvotes: 2

Views: 97

Answers (4)

swapnil kambe
swapnil kambe

Reputation: 123

<?php
 $csvdata   = array("a", "b", "c");
 $apendthis = array("d", "e", "f");

$result1 = array_merge($csvdata,$apendthis);
//print_r($result1);

Array                                                                                                                                                                              
(                                                                                                                                                                                  
    [0] => a                                                                                                                                                                       
    [1] => b                                                                                                                                                                       
    [2] => c                                                                                                                                                                       
    [3] => d                                                                                                                                                                       
    [4] => e                                                                                                                                                                       
    [5] => f                                                                                                                                                                       
)        



$result[] = array_merge($csvdata,$apendthis);
print_r($result);

Output:

Array                                                                                                                                                                              
(                                                                                                                                                                                  
    [0] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [0] => a                                                                                                                                                               
            [1] => b                                                                                                                                                               
            [2] => c                                                                                                                                                               
            [3] => d                                                                                                                                                               
            [4] => e                                                                                                                                                               
            [5] => f                                                                                                                                                               
        )                                                                                                                                                                          

}
?>

Upvotes: 1

Virb
Virb

Reputation: 1578

Have a look at this-

$final = array();   
$result = array(array_merge($csvdata[0], $apendthis[0]));
foreach($result as $key=>$val)
{
    $final['Array'] = $val;
}
echo "<pre>";
print_r($final);

Output will be,

Array
(
    [Array] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )

)

Upvotes: 1

hyubs
hyubs

Reputation: 737

It should be something like this:-

$result = [];

for ($i = 0; $i < count($csvdata); ++$i) {
    // Catch if 2nd array is shorter
    $arr2 = (isset($apendthis[$i])) ? $apendthis[$i] : [];
    $result[] = array_merge($csvdata[$i], $arr2);
}

// Add the remaining end of the 2nd array if it's longer
if (count($csvdata) < count($apendthis)) {
    $result = array_merge($result, array_slice($apendthis, count($csvdata)));
}

var_dump($result);

Upvotes: 1

Ayirp86
Ayirp86

Reputation: 31

Maybe you need to code like this :

<?php
$csvdata   = array("a", "b", "c");
$apendthis = array("d", "e", "f");

print_r(array_merge($csvdata,$apendthis));
?>

Upvotes: 1

Related Questions