I had some problem when to post array data:
When I post the data it will be 2 array:
Array(1)[
[0]=>1
[1]=>2
[2]=>3
]
Array(2)[
[0]=>1
[1]=>
[2]=>3
]
Note that Array2 will be some blank data.
Now I am working until using array_filter(Array2) but the question is can i get the key /data from Array1 match to the not null data from Array2?
Maybe it will be some confusing on question, Sorry to my bad language...
If you want to filter $array1
based on the value of $array2
, you can use filter
with third parameter ARRAY_FILTER_USE_KEY
to use the key.
$array1 = [1,2,3];
$array2 = [1,null,500];
$newArray1 = array_filter($array1, function ($key) use ($array2) {
return $array2[$key];
},ARRAY_FILTER_USE_KEY
);
$newArray2 = array_filter($array2);
echo "<pre>";
print_r( $newArray1 );
print_r( $newArray2 );
echo "</pre>";
This will result as:
Array
(
[0] => 1
[2] => 3
)
Array
(
[0] => 1
[2] => 500
)
You can use array_intersect
to get non-null values from array1
$arr = array(
1 => array(1,2,3),
2 => array(1,'',3)
);
$result= array_filter(array_intersect($arr[1],$arr[2]));
echo '<pre>';
print_r($result);
Result
Array
(
[0] => 1
[2] => 3
)
If my assumption is correct:
Use array_intersect_assoc and array filter.
Array filter will remove null values and array_intersect_assoc will find matching in between the two arrays.
Array_intersect_assoc is what you need since it will match keys also and not only values.
$res = array_intersect_assoc(array_filter($arr1), array_filter($arr2));
See example here: https://3v4l.org/Tp72C Null values are omitted and the "1" is only matched at key 0 since there is no "1" at position 4 in array2