0

Let's say i have an array like this:

$array = array(
  0 => 
    array (
      'value' =>  '1' ,
      'name' =>  'dasdfa sadfa' ),

   1=> Array (
      'value' =>  '[email protected]' ,
      'name' =>  'd2' ),
  21 => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'name1`' ),

  23 => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'POPESCU CATALINA' ),
  24 => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'POPESCU CATALINA' ),

  26 => 
    array(
      'value' =>  '[email protected]',
      'name' =>  '43414 Test01'),
  27 => 
    array(
      'value' =>  '[email protected]',
      'name' =>  'oct oct' )

);

I want to know if exists duplicated value in array with key 'value' I know how to do this if i want a specified value but general no. The result must be an array with no duplicated values(eg:

    $array = array(
  0 => 
    array (
      'value' =>  '1' ,
      'name' =>  'dasdfa sadfa' ),

   1=> Array (
      'value' =>  '[email protected]' ,
      'name' =>  'd2' ),

  23 => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'POPESCU CATALINA' ),

  26 => 
    array(
      'value' =>  '[email protected]',
      'name' =>  '43414 Test01'),
  27 => 
    array(
      'value' =>  '[email protected]',
      'name' =>  'oct oct' )

);`

Please help me.

This is my try

function has_dupes($array){
 $dupe_array = array();
 foreach($array as $val){
  if(++$dupe_array[$val] > 1){
   return true;
  }
 }
 return false;
}
3
  • 2
    please share what you tried then we can help you in modify that with better way.... Commented Nov 12, 2014 at 9:40
  • please provide better description Commented Nov 12, 2014 at 9:43
  • I want to remove the key from "main array" if value from key 'value' is duplicated. Commented Nov 12, 2014 at 10:10

4 Answers 4

2

Try this way:

    $array = array(
  '0' => 
    array (
      'value' =>  '1' ,
      'name' =>  'dasdfa sadfa' ),

   '1'=> Array (
      'value' =>  '[email protected]' ,
      'name' =>  'd2' ),
  '21' => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'name1`' ),

  '23' => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'POPESCU CATALINA' ),
  '24' => 
    array(
      'value' =>  '[email protected]' ,
      'name' =>  'POPESCU CATALINA' ),

  '26' => 
    array(
      'value' =>  '[email protected]',
      'name' =>  '43414 Test01'),
  '27' => 
    array(
      'value' =>  '[email protected]',
      'name' =>  'oct oct' )

);
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
Sign up to request clarification or add additional context in comments.

5 Comments

This is not ok because when i have two values equal but name different doesn't eliminate this!
This is to eliminate duplicate array, if u want to eliminate duplicate with value then use array_filter php.net/manual/en/function.array-filter.php
I don't understand how to use array_filter with your solution
you can define some user defined function which will callback in array_filter. i.e array_filter($array, 'your_function')
Oky that i understood, but the problem is the user callback. I don't really know how to make this :(
1
$result = array_unique($array);
print_r($result);

And if you want to store all unique data in one array do it like this:

//declare $array
$unique_array = array();
foreach ($array as  $key => $type) {
    foreach($type as $vale => $name) {
        if ($vale == 'value') {
            //echo $name . '<br>';
            array_push($unique_array, $name);
        }
    }
}
$result = array_unique($unique_array);
foreach ($result as $res) {
    echo $res . '<br>';
}

4 Comments

@OsomA, you didn't exactly say what you want if there some the same data!
I want to remove the key from main array, you can see the example up!
Thank you, it is ok, but now what i need, i will rewrite myself
Yeap, this is the main idea, understand your "stack" moment, keep in mind, and resolve it by yourself
1

Try this

$values = array_map("unserialize", array_unique(array_map("serialize", $array)));

foreach ($values as $key => $value)
  {
    if ( is_array($value) )
    {
       $values[$key] = $value;
    }
  }

print_r($values);

Comments

0
$unique_data = array(); // the result array
$duplicate_data = array();

$seen = array();
foreach ($array as $key => $arr) {
    $value = $arr['value'];
    if (!isset($seen[$value])) {
        $seen[$value] = '';
        $unique_data[$key] = $arr;
    } else {
        $duplicate_data[$key] = $arr; // optional
    }
}
unset($seen); // optional in function scope

2 Comments

please add some explanation of what your code does and how it solves the problem, this will help others in the future
The code is self-explanatory. But it simply builds a new array, omitting duplicates which occur later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.