How to get total of 2 arrays

Jearson

New Member
#1
How do I get the total of 2 arrays.
Mã:
$array1 = [176, 0];
$array2 = [0, 160];

$combinedArrays = $array1 + $array2;

// Actual result
[176, 0]

// Desired result
[176, 160]
 

Admin

Administrator
Thành viên BQT
#2
You can do something like this
Mã:
$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);
array_filter will take the 0 out and array_merge will merge 2 arrays together.

You can use
Mã:
$array3 = array_values(array_filter(array_merge($array1,$array2)));
to reset the keys.

PHP refer for array_filter and array_merge
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top