Reputation:
So I have two arrays :
1. $users_populated = users_populated: Array
(
[0] => Array
(
[id] => 10000106
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
)
[1] => Array
(
[id] => 10000106
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
[email] =>
[role] =>
[roleids] =>
[platform] =>
)
[2] => Array
(
[id] => 10000065
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
[email] =>
)
[3] => Array
(
[id] => 296
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
[email] =>
)
[4] => Array
(
[id] => 297
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
[email] =>
)
)
2. $user_list: Array
(
[0] => 10000106
[1] => 297
)
So I want the values from 1st array which matches the 2nd array that is Entries :
$output = output: Array
(
[0] => Array
(
[id] => 10000106
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
)
[1] => Array
(
[id] => 297
[username] =>
[name] =>
[firstname] =>
[initial] =>
[surname] =>
[email] =>
)
)
In Short IDs from 1st array and values in 2nd array should match
I have tried using Array intersect key but that didn't work out..
Thanks for reading.
Upvotes: 0
Views: 107
Reputation: 37365
Use array_uintersect()
:
$result = array_uintersect($one, $two, function($x, $y)
{
$x = is_array($x)?$x['id']:$x;
$y = is_array($y)?$y['id']:$y;
return $x-$y;
});
-your second array is plain, thus it will act directly in callback.
Upvotes: 2
Reputation: 184
By looping $users_populated and array_seach function you may obtain the expected result.
<?php
$output = array();
// Case 1: This will overlap by using the last appearance of the same ID value if repeated ID appears in $users_populated
foreach($users_populated as $value) {
if($key = array_search($value['id'], $user_list))
$output[$key] = $value;
}
// Case 2: This will use the first result found in $users_populated
foreach($users_populated as $value) {
if(empty($output[$value['id']]) && $key = array_search($value['id'], $user_list))
$output[$key] = $value;
}
var_dump($output);
Hope this is what you want.
Upvotes: 0
Reputation: 618
When you want to check if two arrays contain the same values, regardless of the values' order, you cannot use "==" or "===". Try this function .
<?php
function array_equal($a, $b) {
return (is_array($a) && is_array($b) && array_diff($a, $b) === array_diff($b, $a));
}
?>
Upvotes: 0