how do i add values from arrays to an array so that it grows by time.
eg.
all values form array1 to myArray.
all values form array2 to myArray.
so now myArray contains all values from 1 and 2.
i want to do this in a cpu efficient way
$myArray = array_merge($array1, $array2);
See the documentation, as there are a few things you will want to know about how duplicates and numerical keys are handled.
Either use the array_merge() function (also see array_merge_recursive()):
$myArray = array_merge($array1, $array2);
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.
Or use the Union Array Operator (+):
$myArray = $array1 + $array2;
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.
Related
I'm trying to create these array maps of values, but I need them to be sorted in a way were different values are dependent on each other being in the same position. You could view them as distribution maps but within each map they "belong" to other values in other arrays. There is a master array which holds the relations between values.
All arrays are 100% identical in length. It's almost like "a table" where each row is the individual array.
These are the arrays I have (pseudo):
$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];
$array3 = [11,12,13,14,15];
And this is my master list of dependencies (basically how they depend on each other):
$sort = [[1,7,13],[2,6,11],[1,8,15]];
As you can see not all values are present. It's only the values being dependent on each other that are present. How can I now achieve my outcome which is theory would be something like this:
$array1 = [1,2,3,4,5];
$array2 = [7,6,8,9,10];
$array3 = [13,11,15,12,14];
Notice how "8" and "15" doesn't really have anything more to map against so they are just left as combined at the end of their own array. This isn't super important, they could be randomized to just keep the first keys of the array intact for as long as it's possible to create the sorting as intended.
I have two arrays with the same keys but different values. I need to merge it but if the values are the same leave only one of this
$array1 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1);
$array2 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_2);
I need to get:
$array_result = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1, 'address'=>$addres_2);
can anybody help to solve this?
array_merge does not work for me..
First you need to merge 2 arrays, using array_merge() function. then get the unique elements from the array using array_unique() function will get you the result
var_dump(array_unique(array_merge($array1, $array2)));
Edit
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.
php doc
Thanks #Marco
Suppose you have two (numeric) arrays $a and $b and want to achieve what you get by doing
$a = & array_merge($a, $b);
efficiently.
Is this already the way to do it?
I find array_merge suspicious because it merges associative keys. Also I suppose the array is not updated but copied unnecessarily.
Is there a way to call array_push with an array?
Or would you do it in a loop with $a[] = ... assignments?
array_merge will reindex keys. To maintain keys, use $c = $array1 + $array2. If $array2 has an existing key, it will be ignored. You can also array_push, array_pop, array_shift, and array_unshift for item addition and subtraction.
I have 2 arrays
First array -- array1(3,17,19,11,34,56,22,29);
Second array -- array2(4,6,12,19,59,21);
Now I would like to get 3 types of data
a) values which are present in both array for eg `19`
b) values which are not present in array1 but present in array 2 for eg `4,6,12,59,21`
c) values which are not present in array2 but present in array 1 for eg `3,17,11,34,56,22,29`
Can it be done using single for loop?
Please suggest.
The PHP docs are your friend
PHP has a bunch of built in functions for working with arrays
The full list is here: http://www.php.net/manual/en/ref.array.php
The ones you're after are array_intersect and array_diff
Look mum, no loop!
a) array_intersect($array1, $array2)
b) array_diff($array1, $array2)
b) array_diff($array2, $array1)
Try array_merge:
array_unique(array_merge($array1, $array2));
It seems to work, but it feels wrong, I assume it is.
Is it wrong?
If so, I currently have an array with keys being mysql database id's and the values being their values.
Would it be better to have the key being "record_"+$id and then explode()ing the key and getting the id from that?
Or is it ok to set your own array keys, and php will just assume they are assoc array keys, rather than indexes?
Thanks
Some built-in PHP functions (like array_merge / array_multisort) will re-index your array:
array_merge() 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. Values in the
input array with numeric keys will be renumbered with incrementing
keys starting from zero in the result array.
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative
(string) keys will be maintained, but numeric keys will be re-indexed.
I would advise you not to do that, use a proper value instead, or at the very least prefix it with a short _:
foreach ($array as $key => $value)
{
$id = ltrim($key, '_');
// do stuff with the actual $id
}