Can I merge arrays which names are kept as variable? - php

I have an array that contains names of arrays
$names_array[] = ('$array1', '$array2', $array3'....)
The $names_array[] is dynamically updated so it may contain 2 or more different names.
When the script is executed the values of listed arrays in $names_array[] need to be merged.

I think in case of merging it is not a problem
u can merge $result = array_merge($array1, $array2);
http://php.net/manual/en/function.array-merge.php

I think it can be done with variable variables.
$arraymerge = array();
foreach ($names_array as $arrayname)
{
$arraymerge = array_merge($arraymerge, ${$arrayname});
}

Thanks for help... I have went around the problem: if anyone needs to merge dynamically generated arrays, in my case I have six arrays that exist or not, so I need to merge the existing ones. What I did is:
if(!is_array($array1[$i])) $array1[$i]=array();
if(!is_array($array2[$i])) $array2[$i]=array();
if(!is_array($array3[$i])) $array3[$i]=array();
if(!is_array($array4[$i])) $array4[$i]=array();
if(!is_array($array5[$i])) $array5[$i]=array();
if(!is_array($array6[$i])) $array5[$i]=array();
$combineddata[$i]=array_merge($array1[$i], $array2[$i],$array3[$i],$array4[$i], $array5[$i], $array6[$i]);
In case 'array_x[$i]' doesn't exists array_merge doesn't break the script just merges empty array.
Thanks

$names_array = array ('array1', 'array2', 'array3');
$array1 = array ('a','b','c');
$array2 = array ('d','e','f');
$array3 = array ('g','h','i');
$result = array ();
foreach ($names_array as $x) {
$result = array_merge ($result, $$x);
}
print_r ($result);

Related

PHP- Create an array with the matches of two arrays

I am trying to compare two non-associative arrays to create a new array with the matches.
This is what I have though:
//This array has several entries
$a_firstarray = Array();
//This array has less entries than the first array
$a_secondarray = Array();
//This array should contain the matches of the first array and the second array in no particular order
$a_mergedarray
for($i=0;$i <=count($a_firstarray);$i++){
for($a=0;$a <=count ($a_secondarray);$a++){
if($a_firstarray[$i] == $a_secondarray[$a]){
$a_mergedarray[] = $a_activecategory[$i];
}
}
}
It doesn't work for some reason. I am also sure that PHP has some sort of function that does this. Any ideas? thanks in advance.
This is known as the "intersection" of two arrays. PHP provides array_intersect.
use array_intersect.
$result = array_intersect($array1, $array2);
Are you looking for array_intersect()?
http://php.net/manual/en/function.array-intersect.php

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

PHP array_chunk and then array_merge

is there any way to merge undefined number of arrays? Array_merge doesn't work for me, cause you have to actually put those arrays as parameters, or maybe there is a way.
I've chunked an array into n - number of arrays, I do some stuff on those chunks and would like to merge some other arrays:
$chunky = array_chunk($positions);
$arraytomerge = array();
foreach($chunky as $key=>$val)
{
do some stuff with $keys and $vals
$arraytomerge[] = array('1','2','3','4');
}
$merged = array_merge($arraytomerge[0],$arraytomerge[1]...);
How to list arrays as array_merge parameters?
Instead of doing
//do some stuff with $keys and $vals
$arraytomerge[] = array('1','2','3','4');
Just do
//do some stuff with $keys and $vals
$merged = array_merge($merged,array('1','2','3','4'));
Or better yet, just add your new items directly to the $merged array instead of creating a new array

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)

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