Merge array in a different way in PHP
Posted
by
user2984974
on Stack Overflow
See other posts from Stack Overflow
or by user2984974
Published on 2013-11-12T21:35:36Z
Indexed on
2013/11/12
21:53 UTC
Read the original article
Hit count: 162
I have these two arrays:
$array1 = array( '0' => 'apple', '1' => '' , '2' => 'cucumber' );
$array2 = array( '0' => '', '1' => 'bmw', '2' => 'chrysler' );
if I do this to merge these arrays:
$result_arr = array_merge($array1, $array2);
print_r( count ( array_filter($result_arr) ) );
the output would be 4.
However, I need to get the number 3. So, when there are two things on the same position (same key) count it only once.
Is it possible to merge/count elements in arrays like that?
© Stack Overflow or respective owner