Home > database >  Filter rows with unique column value and prioritize rows with a particular value in another column
Filter rows with unique column value and prioritize rows with a particular value in another column

Time:12-29

I have an array that needs to be unique and I need to have a condition to check if value dis is in ANSWERED status. If yes then keep it and remove the row that is in NO ANSWER status.

this is my full array

Array
(
    [0] => Array
        (
            [dest] => 960
            [dis] => ANSWERED
        )

    [1] => Array
        (
            [dest] => 596
            [dis] => NO ANSWER
        )

    [2] => Array
        (
            [dest] => 596
            [dis] => ANSWERED
        )

    [3] => Array
        (
            [dest] => 595
            [dis] => NO ANSWER
        )

    [4] => Array
        (
            [dest] => 595
            [dis] => NO ANSWER
        )

)

after removing duplicates:

Array
(
    [dest] => 960
    [dis] => ANSWERED
)
Array
(
    [dest] => 596
    [dis] => NO ANSWER
)
Array
(
    [dest] => 596
    [dis] => ANSWERED
)
Array
(
    [dest] => 595
    [dis] => NO ANSWER
)

with this code :
foreach(array_unique($testArr, SORT_REGULAR) as $doc)
{
    print_r($doc);
}

What I need to do now is remove array[1] that is NO ANSWER because array[2] with key : dis is ANSWERED

CodePudding user response:

You can do it in 2 steps :

<?php

$data = [
    ['dest' => 960, 'dis' => 'ANSWERED'],
    ['dest' => 596, 'dis' => 'NO ANSWER'],
    ['dest' => 596, 'dis' => 'ANSWERED'],
    ['dest' => 595, 'dis' => 'NO ANSWER'],
    ['dest' => 595, 'dis' => 'NO ANSWER'],
];

// Step 1: Filtering step
$intermediate = [];
foreach ($data as $item) {
    if (!isset($intermediate[$item['dest']])) {
        $intermediate[$item['dest']] = $item['dis'];
    } elseif ($item['dis'] === 'ANSWERED') {
        $intermediate[$item['dest']] = $item['dis'];
    }
}

// Step 2: Rebuild to final format
$result = [];
foreach ($intermediate as $key => $value) {
    $result[] = ['dest' => $key, 'dis' => $value];
}

print_r($result);

Gives :

Array
(
    [0] => Array
        (
            [dest] => 960
            [dis] => ANSWERED
        )
    [1] => Array
        (
            [dest] => 596
            [dis] => ANSWERED
        )
    [2] => Array
        (
            [dest] => 595
            [dis] => NO ANSWER
        )
)

CodePudding user response:

While iterating assign temporary keys while pushing rows into your result array. Also, if the dest is not yet represented in the result array or if the dis is ANSWERED, then store the row.

Code: (Demo)

$result =[];
foreach ($data as $row) {
    if (!isset($result[$row['dest']]) || $row['dis'] === 'ANSWERED') {
        $result[$row['dest']] = $row;
    }
}
var_export(array_values($result));  // reindex the result array

CodePudding user response:

You can use array_map function

function fun2($v1)
{
    if ($v1->dis=='ANSWERED') return $v;
}
$finalArray = array_map("fun1", $testArr));
print_r($finalArray)
  • Related