Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can I merge array of arrays to one array in PHP?

I have this Array I wanna compaine it to one array using PHP Array ( [0] => Array ( [choices] => Array ( [choices_1] =>1 [choices_2] =>1 ) ) [1] => Array ( [choices] => Array ( [choices_3] =>2 ) ) ) It Should be like this Array ( [choices_1] =>5 [choices_2] =>5 [choices_3] =>2 )

user-image
Question added by Khadijah Shtayat , Technical Lead , Opensooq
Date Posted: 2013/11/06
Mohammad Ateieh
by Mohammad Ateieh , Software Engineering Manager , Bayt.com

give this a try 

$array = Array (0=>Array("choices"=>Array("choices_1"=>1,"choices_2" =>1)),

                         1 => Array ( "choices" => Array ( "choices_3" =>2))); 

$results = array_merge_recursive($array[0],$array[1]);

echo "<pre>";

print_r($results);

echo "</pre>";

Zaid Rabab'a
by Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.

You can use array_merge()

 

$result array_merge($array1$array2);

Muhammad Zeshan
by Muhammad Zeshan , Web Application Developer , Inquiron

array_merge_recursive($array1, $array2);The above function will do your job. For full reference click

Here. Thanks.

Nouphal Muhammed
by Nouphal Muhammed , Senior Web Developer , Planet Green Solutions

$array = Array (

0=>Array(

"choices"=>Array("choices_1"=>1,"choices_2" =>1)

),

               1 => Array ( "choices" => Array ( "choices_3" =>2))

);

$result=array();

foreach($array as $arr){

 $result  = array_merge($arr['choices'],$result);

Sami Abu Hamam
by Sami Abu Hamam , Senior Full Stack , Vertex Gaming

You can use array_merge()

 

It is simple in use.

 

Awwab Ahmed
by Awwab Ahmed , Web-Developer, Graphic-Designer. , Conobio

By using array_merge funtion

<?php

$array1 = array("color" => "red", 2, 4);

$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);

$result = array_merge($array1, $array2);

print_r($result);

?>

Bahadur Oad
by Bahadur Oad , Senior Software Engineer , Zepcom

use array_merge() function

Faisal Farooq
by Faisal Farooq , Network & Linux System Support Engineer , RED SIGNAL

You can use array_merge function to merge on one or more array to a single array. e.g

Suppose you have2 array array_1 and array_2.

result=array_merge(array_1,array_2).

the resulting array which is in my case is result will contain your all arrays.

<?PHP    $your_array=Array(Array('choices'=> Array('choices_1'=>1,'choices_2'=>1)),Array('choices' => Array('choices_3' =>2)));    $result = array_merge($your_array[0]['choices'], $your_array[1]['choices']);    var_dump($result);?>

array_merge is the way to go. 

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.