I have an array like below. I need to compare with each other and return the matching array on the first index and followed by other.
How can we compare whole array each other based on id and scid with $scid?
$scid = 307;
$array = array(
0 =>
array(
'id' => '485',
'scid' => 306
),
1 =>
array(
'id' => '484',
'scid' => null
),
2 =>
array(
'id' => '486',
'scid' => 305
),
3 =>
array(
'id' => '485',
'scid' => 307
),
4 =>
array(
'id' => '485',
'scid' => 309
),
5 =>
array(
'id' => '485',
'scid' => 329
),
);
The result array should be like
array(3) {
[485]=> array(2) { ["id"]=> string(3) "485" ["scid"]=> int(307) }
[484]=> array(2) { ["id"]=> string(3) "484" ["scid"]=> NULL }
[486]=> array(2) { ["id"]=> string(3) "486" ["scid"]=> int(305) }
}
If array has duplicate id on which scid is not matching then we can pick any id value. Note :The matching sub array should always be the first index of resulting array.An amount will always be unique and might contain null as well in array.