0

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Yogs
  • 78
  • 7
  • Well you have done all the analysis. Now you just need to code something to do it. – RiggsFolly Feb 22 '17 at 11:51
  • *duplicate id on which scid is not matching then we can pick any id value* - we can capture the first occured value, right? like `[485]=> array(2) { ["id"]=> string(3) "485" ["scid"]=> int(306)` (first amoung `485`) – RomanPerekhrest Feb 22 '17 at 12:00
  • In the above scenario 485 has matching 307, so it must return [485]=> array(2) { ["id"]=> string(3) "485" ["scid"]=> int(307). If it doesnot have that matching then it can pick any one with id 485. Also the one with matching scid should always be in first index. – Yogs Feb 23 '17 at 04:14
  • I found a solution on http://stackoverflow.com/questions/12559878/multidimensional-array-find-item-and-move-to-the-top Thanks – Yogs Feb 28 '17 at 03:32

2 Answers2

0

Try this,

$temp = [];
    foreach($array as $k => $v){
        $temp[$v['id']][] = $v;
    }

Give it a try, this will work.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You want something like this?

$result = [];
foreach ($array as $item) {
    $result[$item['id']] = ['id' => $item['id'], 'scid' => $item['scid']];
}
var_dump($result);
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • Not really, , it doesnot have any comparision withe the $scid variable.It will just remove the duplicate but not have an array with matching $scid. – Yogs Feb 23 '17 at 04:19
  • I have a solution to compare values, but I dont know how to keep the matching subarray always on the top. $scid=307; $result = []; for($i = 0; $i < sizeof($array); $i++){ if (!array_key_exists($array[$i]['id'], $result)|| $result[$array[$i]['id']]['scid'] != $scid) { $result[$array[$i]['id']] = $array[$i]; } } – Yogs Feb 23 '17 at 04:24