Need to merge arrays but leave only one value if duplicated - php

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

Related

Does array_merge preserve order?

I have two arrays :
$arr = ["ham", "beef", "testing1"];
$arr1 = ["baby", "chicken", "wax"];
When merging them I get the following result :
var_dump(array_merge($arr, $arr1));
// ["ham", "beef", "testing1", "baby", "chicken", "wax"]
As you can see, the order is kept and they're added at the end of the first array. Can I be SURE that this is ALWAYS the case? Or the order is not necessarily kept? I found nothing in the docs about the order of the results.
You can read more in documentation:
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
So as you can see the values of second array are appended to the end of the previous one.
For duplicate keys the last one will override the previous one as it states in documentation:
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.

Can someone explain why array_merge() won't merge these two seemingly identical arrays?

I have the below snippet of code and I can't figure out why these two arrays won't merge - likely due to not understanding 100% how array_merge works. I'm expecting to see the duplicate, and merge the two into a single key. Running array_unique results in errors. Any ideas, or possibly an alternate solution to merge these two?
$a['12345']['label'] = '12345';
$a['12345']['type'] = 'Newspaper';
$b['12345']['label'] = '12345';
$b['12345']['type'] = 'Newspaper';
$result = array_merge($a, $b);
echo "<pre>";
print_r($result);
From the docs
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.
The short answer is that the merge is occurring, just not in the way you desire.
Because they would have the same keys. Try changing $b['12345'] to $b['12346'] and see if they merge.

How to check an associative array for all null values in php?

This is probably a very trivial question, but please bear with me.
I am trying to read a lot of data into an array of associative arrays. The data contains a lot of empty arrays and arrays with keys set and but all null values. I want to ignore those and only push arrays with at least one key mapped to a non-null value. (The data comes from an excel sheet and it has lots of empty cells that are registered as "set" anyway.) So far I have tried:
if(!empty(${$small_dummy}))
array_push(${$big_dummy}, ${$small_dummy});
That gets rid of the empty arrays but not the ones where all keys map to null. Is there a better way to do this than looping through the entire array and popping all null values?
Judging by the code you have already, you can change:
if(!empty(${$small_dummy}))
to:
if(!empty(array_filter(${$small_dummy})))
That will filter out all empty values (values evaluating to FALSE to be precise) and check if the resulting array is empty. Also see the manual on array_filter().
Note that this would also filter 0 values so you might need to write a custom callback function for array_filter().
You can try if(!array_filter($array)) { also
This isn't an ideal approach, but array_sum will return 0 if all values can't be cast to a numeric value. So :
$small_dummy = array("a" => null, "foo", "", 0);
if(array_sum($small_dummy) === 0)
would pass. But this is only the way to go if you are expecting the values to be numeric.
Actually, if the problem is that the array keys have values and therefor are not passing as empty(), the go with array_values:
if(!empty(array_values(${$small_dummy})))

Using numbers as assoc array keys

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
}

add values from arrays to an array?

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.

Categories