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);
Related
I have three array from dynamic data in php for example :
array1 = array('10-11-2015'=>23.6,
'25-11-2015'=>48.6,
'12-11-2015'=>14.52);
array2 = array('10-11-2015'=>8.7,
'22-10-2015'=>86.6,
'12-11-2015'=>78.5);
array3 = array('10-11-2015'=>5.8,
'19-09-2015'=>3.6,
'12-11-2015'=>96.4);
I just need common keys from this three array
newarray = array('10-11-2015','12-11-2015');
I use array_intersect_key but it is for only two array.
How I can get it form three or more array ?
Something like this?
array_keys(array_intersect_key($array1, $array2, $array3));
I have problem wiht array_merge():
First array:
$array1=array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",["key3"]=>"value3")
);
Second array:
$array2=array(["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");
And I need merge this arrays to one like this:
$array1=array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",
["key3"]=>"value3",["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6"));
But when use:
$array3=array_merge($array1,$array2);
var_dump($array3);
var_dump return this:
array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",
["key3"]=>"value3") ["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");
And don't know why.
Thanks
$array3=array(array_merge($array1[0],$array2));
you have to merge the inner array, not the outer one.
https://3v4l.org/dCm2F
Merging the first element from first array with the second one, may help:
$array3 = array();
$array3[0] = array_merge($array1[0], $array2);
How can I unset the keys in one array where the values contained in a second array match the values in the first array?
Actual array:
$fruits = array('Banana','Cherry','Orange','Apple');
Elements I want to remove:
$remove = array('Banana','Apple');
Need to return:
$array = array('Cherry','Orange');
I know it's possible to remove each one with unset, but I'm looking to make it in one line with two array.
Thanks.
Take a look at this function
link
$arrayWithoutTheDesiredElements = array_diff($originalArr, $toRemoveArray)
EDIT:
for your case: $array = array_diff($fruits, $remove);
I have two arrays, like this:
$array1 = array(
'ADAIR',
'ADAM',
'ADAMINA',
'ADDISON',
'ADDY',
'ADELLE',
'ADEN',
'ADOLPH',
'ADRIANNA'
);
$array2 = array(
'ADAIR',
'ADAMINA',
'ADRIANNA'
);
How do I make a third array, without duplicates? We should take first array and remove from it duplicates from second array.
Use Array-diff
$array3=array_diff($array1,$array2);
Returns an array containing all the entries from array1 that are not present in any of the other arrays.
Take a look here: http://php.net/manual/en/function.array-unique.php
Combine both arrays into 1, then run them through the array-unique function
$result = array_unique($combined);
#grunk deleted a perferctly valid answer, so credits not to me:
$unique = array_unique(array_merge($array1,$array2));
codepad.org/NVkuml5g
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