2

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 
    ) 
)
2
  • Did you try array_merge_recursive? Commented Aug 2, 2017 at 8:37
  • try array_merge? Commented Aug 2, 2017 at 8:37

4 Answers 4

1

Maybe you need to code like this :

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

print_r(array_merge($csvdata,$apendthis));
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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
        )

)

Comments

1
<?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                                                                                                                                                               
        )                                                                                                                                                                          

}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.