1

I have a multidimensional array and I want to remove a array record on duplication of specific index value and I have to use a condition as well before discarding a record.

for example my array is as below

array { 
 [0]
{ ["name"]=> string(12) "A1"   ["status"]=> string(16) "Not Available"   ["statusid"]=>4 ["num"]=> string(7) "33" } 

[1]
{ ["name"]=> string(15) "A2"   ["status"]=> string(16) "Available"  ["statusid"]=>1  ["num"]=> string(7) "39" } 

[2]
{ ["name"]=> string(21) "A3"   ["status"]=> string(16) "Busy" ["statusid"]=>3 ["num"]=> string(7) "55" } 

[3] 
{ ["name"]=> string(12) "A4"   ["status"]=> string(16) "Available"  ["statusid"]=>1 ["num"]=> string(7) "54" } 

[4]
{ ["name"]=> string(18) "A5"   ["status"]=> string(16) "Busy"  ["statusid"]=>3 ["num"]=> string(7) "33" } 
}

In above array I want to check duplication on "num" if it repeats then I want to remove the record which has higher "statusid" like in above record "num" is repeating at [0][num] and [4][num] but i want to keep row [4] as its [4][statusid] is less then [0][statusid].

Kevin
  • 41,694
  • 12
  • 53
  • 70
F Q
  • 97
  • 6

3 Answers3

1

Just use a simple if. You'll need to transfer it inside another container. Just check if its already set, if it is, just check the num:

$new_array = array();
foreach($array as $a) {
    if(!isset($new_array[$a['num']]) || $a['statusid'] < $new_array[$a['num']]['statusid']) {
        $new_array[$a['num']] = $a;
    }   
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • thanks for your help but i dont want this out put. what I want is to check duplication of num and if find then remove that array which has higher value of statusid. like in my array [1],[2],[3],[4] will remain in array but [0] will remove as it has same [0][num] as [4][num] but [0][statusid] has higher value then [4][statusid] there fore it should remove from array. hope you understand now. – F Q Sep 23 '16 at 06:37
  • @FQ got the indices jumbled, the edit should work correctly – Kevin Sep 23 '16 at 06:43
  • Thanks working but how to save array with index values like [0],[1],[2],[3] etc it is saving with status id – F Q Sep 23 '16 at 07:29
  • @FQ just use `array_values` to reindex the array `$new_array = array_values($$new_array);` – Kevin Sep 25 '16 at 21:47
0

This is one liner code that you can use it:

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

Virb
  • 1,639
  • 1
  • 16
  • 25
0

you have to do two things: first compare the rows in the table and create a function that copy one array to another exept a specific row.

$k=0;
for($i=0;$i<$K;$i++)//fix one row
{
for($j=i+1;$j<$k;$j++)//compare with others rows
{
if($array[i]["num"] == $array[j]["num"])
{
if($array[i]["statusid"]< $array[j]["statusid"]
{
$array = copyTable($k,$j,$array);
$k--;//array length will be decremented by 1
}
else
 {
 $array = copyTable($k,$i,$array);
 $k--;//array length will be decremented by 1
 $i--;//fixed row will be delete, so stay in same position
}
}
}
 }

 function copyTable($tableLength,$indexOfRowToRemove,$previousArray)
 {
 $newArray = array();
 for($i=0;$i<$tableLength;$i++)
 {
 if($i!=$indexOfRowToRemove)
  {
    $newArray[$i] =    array( 
                  'name' => $previousArray[$i]["name"],
                  'status' => $previousArray[$i]["status"],
                  'statusid' =>$previousArray[$i]["statusid"], 
                  'num' => $previousArray[$i]["num"],

              )
  }
 }
 return $newArray;
 }
A.Tarhini
  • 72
  • 4