What is the most efficient way to append an array in PHP - php

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.

Related

PHP Concatenating arrays (lists) and ignoring duplicate keys, because we just use values

We have a list (array) in php that is setup like
$update_product_ids = array();
array_push($update_product_ids, (int)$product->getId()); // (int)$product->getId() is an integer
The I tried:
array_push($update_product_ids, array_values($child_ids)); // child_ids is an array of integers
and
array_merge($update_product_ids, $child_ids); // child_ids is an array of integers
This did not work and it looks like the keys are being merged in either example instead of being added to the end. I think this is because php does not store arrays as ('A', 'B') but rather as (0=>'A',1=>'B') and the 2 arrays I am merging both have keys 0 and 1.
So I resolved to
foreach ($children_ids as $child_id) {
array_push($update_product_ids, (int)$child_id);
}
Which feels a little silly because there must be a way to do this correct in 1 go?
question: how can I merge the above arrays in 1 go?
You can achieve what you want with array_merge. Unlike array_push, array_merge does not modify the arrays provided. It rather returns a new array that is the concatenation of the arrays provided. So basically, do something like:
$update_product_ids = array_merge($update_product_ids, $child_ids);
If you are using PHP 5.6 (or greater), you can also use the "argument unpacking":
array_push($update_product_ids, ...$child_ids);
If you are using PHP 7.4 (or greater), you can use the "spread operator" (same as argument unpacking, but for arrays):
$update_product_ids = [...$update_product_ids, ...$child_ids];

Need to merge arrays but leave only one value if duplicated

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

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.

delete elements in an array and adjust index

Is there a way in php to remove elements in an array and then reindex the remaining elements? For example, here is what I want to do. In an array,
$a = array("a","b","c");
I want to delete element "b", I used unset() to do that leaving the array like ("a",null,"c"). What I really want is make the array ("a","c") after deleting "b". How can I do that? Thanks!
unset does not create null elements in your array. The array will be one element smaller than before.
If you want to reindex the array after removing an element, use $array = array_values($array);.
do you want to do something like
$new_array = array_filter($a)
? You can read about array filter function and take a look at the case without callback parameter (as in my example)
unset($a[1]);
$a = array_values($a);

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