How do I combine these two arrays into a single array say $array3?
$array1 =[1,2,3];
$array2=['a'=>1,'b'=>2,'c'=>3];
Try this
$temp = array_slice(array_keys($array1), 0, count($array2));
$array3 = array_merge($array1, array_combine($temp, $array2));
I believe this is what you're asking.
Related
I'm having 2 arrays:
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
I want to merge $arr_1 and $arr_2 with out exists item in both of these arrays like:
$arr_merged = ['1','3','5','6','7']
How could I do that? I think I have to run some foreach to loop through these arrays and handle, but it's looks like not a really good resolution for this problem...
For getting unique items, the array_diff() function can be used to find the difference between two arrays. It computes the difference of all the arrays and returns all the arrays that do contains entries that cannot be matched in any of the arrays. And for merging two arrays you can use array_merge() function.
<?php
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
$arr_merged = array_merge(array_diff($arr_1, $arr_2), array_diff($arr_2, $arr_1));
print_r($arr_merged);
?>
You use array_diff along with array_merge to achieve the output.
Note: array_diff is technically not the opposite of array_intersect.
<?php
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
$diff_1 = array_diff($arr_1, $arr_2);
$diff_2 = array_diff($arr_2, $arr_1);
$array_merge = array_merge($diff_1,$diff_2);
print_r($array_merge);
?>
I need a function to solve this issue.
Example:
$ar1 = array("alpha" => array("A","B","C","D"), ...);
$ar2 = array("numerics" => array("1","2","3","4"), ...);
$output = merge_arrays($ar1,$ar2);
print_r($output);
Result:
array("A","B","C","D","1","2","3","4");
$output = call_user_func_array('array_merge',
array_values(array_merge_recursive($ar1,$ar2)));
print_r($output);
You can merge this 2 arrays like this:
$new_array = array_merge($ar1['alpha'], $ar2['numerics']);
In this way you will have an array like the one you describe.
You can check more information about array_merge here: http://php.net/manual/en/function.array-merge.php
I have 4 arrays for input errors like:
$username_errors=array();
$email_errors=array();
$password_errors=array();
$errors=array();
I want to merge all the arrays values in $errors array and count if $errors is empty then proceed.
Thanks in advance.
use array_merge
$arr = array_merge($arr1, $arr2, $arr3);
$array3 = array_merge($array1, $array2);
If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:
$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');
If I use array_merge:
$new_array = array_merge($array1, $array2);
I get:
array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')
whereas I want to get something like:
array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))
Create a new array and add the other arrays to that one like this:
$arr = array($array1, $array2);
$ new_array = array ($ array, $ array2);
I have 2 arrays , $array1 & $array2. I want to create new array such that its key value is values of array1 and values are values of array2. Is it possible..
I used following approach to create this new array named $inputs. Is this correct?
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_fill("$array1",count($array1),$array2);
print_r($inputs);
If I read your question correctly, you can use array_combine() PHP Manual
$array1 = array("3","4","6");
$array2 = array("a","b","c");
$inputs = array_combine($array2, $array1); # keys, values
Use array_combine.
http://www.php.net/manual/de/function.array-combine.php
$inputs = array_combine($array1, $array2);
<?php
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_combine($array1,$array2);
print_r($inputs);
?>
http://codepad.org/6fTXCZa5
use php array_combine function
http://php.net/manual/en/function.array-combine.php