php - Adding an element form one array into another - php

I have 2 arrays like so:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2")
);
$array2 = array(
array("foo4"=>"bar","count"=>"3"),
array("foo5"=>"bar2","count"=>"4"),
array("foo6"=>"bar3","count"=>"2")
);
how can i add the 3rd element of array2 into array1 so it can become like this:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2"),
array("foo6"=>"bar3","count"=>"2")
);
i have tried doing $array1 += $array2[2]; but it doesn't work. it just adds the keys from array("foo6"=>"bar3","count"=>"2") to array1 instead of adding it as an array in $array1
Could you help me out?

The [] operator appends an element to the end of an array, like this
$array1[] = $array2[2];

Just do like this:
$array1[] = $array2[2];

If you want the exact 3rd item, then you could do something like:
$array1[] = $array2[2];
If you want the last item of the array, you can use:
$array1[] = $array2[count($array2)];

try this
$array1[] = $array2[2];

array_merge() is a function in which you can copy one array to another in PHP.
http://php.net/manual/en/function.array-merge.php

Related

Array_merge() two array into one not working

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

Put multiple PHP arrays inside another array maintaining keys including duplicates

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

Add tems to array with keys in PHP

How to add array items to an existing array with key => value ? actually i want to create an array of mysql rowset i.e.
$n =0;
while($row = mysql_fetch_array($rowset))
{
$array[$n] = array('name' => $row['name'], 'city' = $row['city']);
$n += 1;
}
Thanks.
Just try with:
$existingArray['newKey'] = 'new value';
Or use array_merge function:
$newArray = array_merge($existingArray, $additionalData);
http://php.net/manual/en/function.array-merge.php
That what you're looking for?
-edit-
Just to note, if conflicting results are found, the last most array entry will be used. If you array merge three arrays with id fields, only the final arrays id will be stored in the result.
You may want to look into this:
http://php.net/array_push
Should be simple enough.
For One:
$array['key'] = $value;
Merge:
$mergedArray = array_merge($array1, $array2);
(http://php.net/manual/en/function.array-merge.php)

Remove duplicates from array

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

Combine Two Arrays with numerical keys without overwriting the old keys

I don't want to use array_merge() as it results in i misunderstood that all values with the same keys would be overwritten. i have two arrays
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
and would like to combine them resulting like this
array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');
array_merge() appends the values of the second array to the first. It does not overwrite keys.
Your example, results in:
Array (
[0] => foo
[1] => bar
[2] => bar
[3] => foo )
However, If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Unless this was just an example to another problem you were having?
Does this answer your question? I'm not sure exactly what you're trying to accomplish, but from your description it sounds like this will work:
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
foreach ($array2 as $i) {
$array1[] = $i;
}
echo var_dump($array1);
If someone stumbles upon this, this is a way to do it nowadays:
var_dump(array_merge_recursive($array1, $array2));
There are probably much better ways but what about:
$newarray= array();
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
$dataarrays = array($array1, $array2);
foreach($dataarrays as $dataarray) {
foreach($dataarray as $data) {
$newarray[] = $data;
}
}
print_r($newarray);
$result = array_keys(array_merge(array_flip($array1), array_flip($array2)));
var_dump($result);

Categories