I am trying to filter 2d arrays which satisfy two conditions. But, it not works as expected. I figured out that error is in array_intersect()
function. Why array_intersect()
not works correctly here.Is there another way to do this without iteration.
<?php
error_reporting(0);
$students = [
["name"=> 'k. l.james', "grade"=>8],
["name"=> 'k. l.james', "grade"=>9],
["name"=> 'e. musk', "grade"=>8],
["name"=> 'jone', "grade"=>9],
];
function filterByGrade($grade){
global $students ;
if (empty($grade)){
return $students ;
}else{
return array_filter($students , function($record) use($grade){
return ($record['grade'] == $grade);
});
}
}
function filterByName($name){
global $students;
if (empty($name)){
return $students;
}else{
return array_filter($students, function($record) use($name){
return (strcasecmp(str_replace(' ','',$record['name']),str_replace(' ','',$name)) == 0);
});
}
}
print_r(filterByGrade(8));
echo "<br/>";
print_r(filterByName('k.l.james'));
echo '<br/>';
print_r(array_intersect(filterByGrade(8), filterByName('k.l.james')));
?>
results are;
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [2] => Array ( [name] => e. musk [grade] => 8 ) )
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [1] => Array ( [name] => k. l.james [grade] => 9 ) )
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [2] => Array ( [name] => e. musk [grade] => 8 ) )
I expect the last line of result as,
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ))
If I interchanged the two arrays in array_intersect() as follows, the results are different.
print_r(array_intersect(filterByName('k.l.james'),filterByGrade(8)));
Then,I get result as follows.
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [2] => Array ( [name] => e. musk [grade] => 8 ) )
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [1] => Array ( [name] => k. l.james [grade] => 9 ) )
Array ( [0] => Array ( [name] => k. l.james [grade] => 8 ) [1] => Array ( [name] => k. l.james [grade] => 9 ) )
I noticed that only the first filtered array in array_intersect()
is printed despite of considering the intersection of the two arrays.
My Answer:
The `array_intersect()` function in PHP is used to find the intersection of two or more arrays. However, it works with one-dimensional arrays and may not work as expected with multi-dimensional arrays.
If you want to filter 2D arrays based on two conditions without iteration, you can use the `array_filter()` function along with a custom callback function. Here's an example:
php
// Sample 2D array
$twoDArray = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30],
['name' => 'Alice', 'age' => 20],
];
// Define the conditions
$condition1 = 'name';
$condition2 = 'age';
// Filter the 2D array based on the conditions
$filteredArray = array_filter($twoDArray, function($item) use ($condition1, $condition2) {
return $item[$condition1] == 'John' && $item[$condition2] == 25;
});
print_r($filteredArray);
In this example, the `array_filter()` function is used to filter the 2D array based on the conditions specified in the callback function. You can modify the conditions as needed to filter the array based on your requirements.
Rate this post
5 of 5 based on 5573 votesComments