0

I have an array as:

$check = [
   0 => [
     'id' => '1',
     'value' => 'true'
   ]

   1 => [
     'id' => '1',
     'value' => 'false'
   ]

   2 => [
     'id' => '1',
     'value' => 'true'
   ]

   3 => [
     'id' => '2',
     'value' => 'true'
   ]
]

Now I want to convert this array into

$check = [
   0 => [
     'id' => '1',
     'value' => 'true'
   ]

   1 => [
      'id' => '2',
      'value' => 'true'
   ]
]

i.e if the indexes of my check[] has same id value then delete all of them except for any one.

abhit
  • 973
  • 3
  • 17
  • 40

1 Answers1

1

Firstly, you have a syntax error in your array. Missing , in between arrays and I have corrected your array. See the code below.

The php function you're looking for is array_unique().

Pass SORT_REGULAR flag as function's second parameter, it means compare items normally (don't change types)

Check your validated code here.

NOTE: Don't forget to select Php compiler from the dropdown.

<?php

$check = [
   0 => [
     'id' => '1',
     'value' => 'true'
   ],

   1 => [
     'id' => '1',
     'value' => 'true'
   ],

   2 => [
     'id' => '1',
     'value' => 'true'
   ],

   3 => [
     'id' => '2',
     'value' => 'true'
   ]
];
print_r(array_unique($check, SORT_REGULAR));
Tahir Raza
  • 726
  • 1
  • 7
  • 20
  • if i change the `'value'=>'true'` to `'value'=>'false'` at index 1 then it will not be deleted but i want to delete that too. – abhit May 25 '17 at 11:49
  • @lakshay Why don't you remove that particular element from the array when ```value=='false'``` & follow the above mention procedure to get the unique result. – Suresh May 25 '17 at 11:54