Using Array_Intersect on a Multi-Dimensional Array

Using array_intersect on a multi-dimensional array

Use array_uintersect() to use a custom comparison function, like this:

$arr1 = array(
array('name' => 'asdfjkl;', 'value' => 'foo'),
array('name' => 'qwerty', 'value' => 'bar'),
array('name' => 'uiop', 'value' => 'baz'),
);

$arr2 = array(
array('name' => 'zxcv', 'value' => 'stuff'),
array('name' => 'asdfjkl;', 'value' => 'foo'),
array('name' => '12345', 'value' => 'junk'),
array('name' => 'uiop', 'value' => 'baz'),
);

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
print_r($intersect);

function compareDeepValue($val1, $val2)
{
return strcmp($val1['value'], $val2['value']);
}

which yields, as you would hope:

Array
(
[0] => Array
(
[name] => asdfjkl;
[value] => foo
)

[2] => Array
(
[name] => uiop
[value] => baz
)

)

Array_intersect an array against a multidimensional array

To match your example output, you can simply use a foreach loop. In your example, the 2D array is $array1 and the 1D array is $array2.

$output = [];

foreach ($array1 as $array) {
$output[] = array_intersect($array, $array2);
}

Note that declaring an array with [] is only supported in PHP versions >= 5.4. For PHP versions < 5.4:

$array1 = array(array(20, 36, 42), array(21, 42, 30));
$array2 = array(24, 42, 26, 12);

$output = array();

foreach ($array1 as $array) {
$output[] = array_intersect($array, $array2);
}

Get intersection of a multi-dimensional array in PHP

You can use array_intersect to cover the dynamic $data as such:

$data = array (
'role_1' =>
array (
0 => 'value_2',
1 => 'value_3',
),
'role_2' =>
array (
0 => 'value_1',
1 => 'value_2',
),
'role_3' =>
array (
0 => 'value_2',
1 => 'value_3',
)
);

$result = call_user_func_array('array_intersect', $data);

call_user_func_array will help spread the elements of your array as parameters inside array_intersect.

PHP Array_intersect on multidimensional array with unknown number of keys

One of the possible solutions: you may first extract() the $realfilters array values to variables, and then apply the array_intersect() to them. But this solution is applicable only if there are not many possible filters.

Another one and probably the best solution would be to intersect in a loop, something like:

$res_arr = array_shift($realfilters);
foreach($realfilters as $filter){
$res_arr = array_intersect($res_arr, $filter);
}

PHP multi dimensional array intersect

array_intersect() is not recursive, it sees the inner arrays as just an array. You would need to use something like this:

function array_intersect_recursive() {

foreach(func_get_args() as $arg) {
$args[] = array_map('serialize', $arg);
}
$result = call_user_func_array('array_intersect', $args);

return array_map('unserialize', $result);
}

$result = array_intersect_recursive($aArray, $jump);

array_intersect Inside multidimensional-array

I copyed you example and for me it works:

<?php

$array = array(
'php' => array(
36,
51,
116,
171,
215,
219,
229,
247,
316,
),
'java' => array(
14,
16,
19,
24,
25,
26,
29,
31,
33,
34,
35,
36,
37,
40,
45,
49,
51,
),
'ajax' => array(
91,
110,
113,
172,
),
);

$intersected_array = call_user_func_array('array_intersect',$array);

print_r($intersected_array);

// RESULT: "Array ( ) "

Please copy exactly this code and tell me your output.

Filter 2D associative array using keys from multiple levels of another 2D associative array

function recursive_array_intersect_key(array $array1, array $array2) {
$array1 = array_intersect_key($array1, $array2);
foreach ($array1 as $key => &$value) {
if (is_array($value) && is_array($array2[$key])) {
$value = recursive_array_intersect_key($value, $array2[$key]);
}
}
return $array1;
}

Demo here.

array_intersect() with 2d arrays in php

You can create a function to filter students using multiple conditions, instead of trying to combine the 2 results.

The function :

/**
* Here, $condition array of keys/values used to filter $data.
* ex: ['name' => 'jone', 'grade' => 9]
*/
function filterArray($data, $conditions)
{
if (empty($conditions)) {
return $data;
}

return array_filter($data, function($record) use ($conditions) {
// Check all given conditions
foreach ($conditions as $key => $value) {
// If doesn't match, return false (don't keep in filtered array)
if ($record[$key] != $value) return false;
}
// conditions passed, add to array
return true;
});
}

Usage :

$students = [
["name"=> 'k. l.james', "grade" => 8],
["name"=> 'k. l.james', "grade" => 9],
["name"=> 'e. musk', "grade" => 8],
["name"=> 'jone', "grade" => 9],
];

print_r(filterArray($students, ['grade' => 8]));
// out : [["name"=> 'k. l.james', "grade" => 8],["name"=> 'e. musk', "grade" => 8]]

print_r(filterArray($students, ['name' => 'k. l.james']));
// out : [["name"=> 'k. l.james', "grade" => 8], ["name"=> 'k. l.james', "grade" => 9]]

print_r(filterStudents($students, ['grade' => 8, 'name' => 'k. l.james']));
// out : [["name"=> 'k. l.james', "grade" => 8]]

Additional notes :

  • using global is discouraged, in the code below, $students are given by the function parameter.
  • No need to use the else statement after a "return early" pattern (if ($someCondition) { return; } else { }).

Intersecting multidimensional array of varying size

This would be simple if your arrays contained only integers, but as they contain an another array, it gets a bit more complicated. But this should do it:

function custom_intersect($arrays) {
$comp = array_shift($arrays);
$values = array();

// The other arrays are compared to the first array:
// Get all the values from the first array for comparison
foreach($comp as $k => $v) {
// Set amount of matches for value to 1.
$values[$v['id']] = 1;
}

// Loop through the other arrays
foreach($arrays as $array) {
// Loop through every value in array
foreach($array as $k => $v) {
// If the current ID exists in the compare array
if(isset($values[$v['id']])) {
// Increase the amount of matches
$values[$v['id']]++;
}
}
}

$result = array();

// The amount of matches for certain value must be
// equal to the number of arrays passed, that's how
// we know the value is present in all arrays.
$n = count($arrays) + 1;
foreach($values as $k => $v) {
if($v == $n) {
// The value was found in all arrays,
// thus it's in the intersection
$result[] = $v;
}
}
return $result;
}

Usage:

$arrays = array(
array(array('id' => 3), array('id' => 1), array('id' => 2), array('id' => 5), array('id' => 4)),
array(array('id' => 1), array('id' => 3), array('id' => 4), array('id' => 5)),
array(array('id' => 3))
);

print_r(custom_intersect($arrays));

Result:

Array
(
[0] => 3
)

This function isn't perfect: if you have duplicate ID's in one array, it will not work. That would require a bit more code to first make the array values unique, but this will probably work in your case.



Related Topics



Leave a reply



Submit