0

I'm trying to count the values in a php array and eliminate repetitions have leaving only 1. The format of my arrangement is as follows:

Array
(
[0] => Array
    (
        [inscripcion_id] => 3932
        [nombre] => Prueba
        [email] => [email protected]
    )

[1] => Array
    (
        [inscripcion_id] => 3926
        [nombre] => Prueba
        [email] => [email protected]
    )

[2] => Array
    (
        [inscripcion_id] => 3921
        [nombre] => Nicolas
        [email] => [email protected]
    )
)

I look matches are based on the email field, if the email appears more than 1 time in the arrangements must be eliminated leaving only 1. The problem is I can not get as often appears no less eliminate it. Try array_count_values does not work, I can find the matches of each email but only in the subarray therefore always find 1 each as follows for example:

Array ( [3932] => 1 [Prueba] => 1 [[email protected]] => 1 ) 
Array ( [3926] => 1 [Prueba] => 1 [[email protected]] => 1 ) 

I hope the result is this:

Array
(
[0] => Array
    (
        [inscripcion_id] => 3932
        [nombre] => Prueba
        [email] => [email protected]
    )

[1] => Array
    (
        [inscripcion_id] => 3921
        [nombre] => Nicolas
        [email] => [email protected]
    )
)
2

4 Answers 4

1

This Should do it.

print_r(unique_multidim_array($array, 'email'));

function unique_multidim_array($array, $key){
    $temp_array = array();
    $i = 0;
    $key_array = array();

    foreach($array as $val){
        if(!in_array($val[$key],$key_array)){
            $key_array[$i] = $val[$key];
            $temp_array[$i] = $val;
        }
        $i++;
    }
    return $temp_array;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this to make an array index by the email (like a primary key).

$data = array(
    array(
        'inscripcion_id'=>3932,
        'nombre'=>'Prueba',
        'email'=>'[email protected]'
    ),
    array(
        'inscripcion_id'=>3926,
        'nombre'=>'Prueba',
        'email'=>'[email protected]'
    ),
    array(
        'inscripcion_id'=>3921,
        'nombre'=>'Nicolas',
        'email'=>'[email protected]'
    ),
);

$result = array();

foreach($data as $key => $value) {
    if (!in_array($value['email'], $result)) {
        $result[$value['email']] = $value;
    }
}

var_dump($result);

Result:

array (size=2)
  '[email protected]' => 
    array (size=3)
      'inscripcion_id' => int 3926
      'nombre' => string 'Prueba' (length=6)
      'email' => string '[email protected]' (length=16)
  '[email protected]' => 
    array (size=3)
      'inscripcion_id' => int 3921
      'nombre' => string 'Nicolas' (length=7)
      'email' => string '[email protected]' (length=15)

Also you can have direct access to every sub array you want using it's email value:

var_dump($result['[email protected]']);

Result:

array (size=3)
  'inscripcion_id' => int 3926
  'nombre' => string 'Prueba' (length=6)
  'email' => string '[email protected]' (length=16)

Comments

0

Just create the result array with the email as the key, then there are no duplicates:

foreach($array as $value) {
    $result[$value['email']] = $value;
}

Then if you want to reset the keys:

$result = array_values($result);

Comments

0

Create an array with the email fields and remove from source array if duplicate:

$temp  = array();
$out   = array();
foreach( $array as $k => $v ) {
    if ( isset( $temp[$v['email']] ) )
        unset( $array[$k]);
    else {
        $temp[$v['email']] = $k;
        array_push( $out, $v );
    }
}

The original array is in $array and the output in $out

If the key is not important an easier way is

$temp  = array();
foreach( $array as $k => $v ) {
    if ( isset( $temp[$v['email']] ) )
        unset( $array[$k]);
    else
        $temp[$v['email']] = $k;
}

Comments

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.