Oscar Paredez
Oscar Paredez

Reputation: 73

Select all the keys inside arrays (inside another array) that contain the same value

My point here is, some keys with the value "nit" are have the same value in many arrays (such as arrays 21, 23 and 24). I want to select all the arrays that have the same "nit" and create a new array with all of those. I need one array (from all the ones with same "nit") to remain in the main array(randomly pick one).

[20] => Array
            (
                [fecha] => 2018-08-27 18:38:49
                [id_cliente] => 
                [nit] => 50255872726
                [destino] => Copartes.Gt
                [nombre] => Registrar Cliente
                [telefono] => 55872726
                [type] => IN
                [mensaje] => Mandame el número de cuenta y te voy a depositar
            )

        [21] => Array
            (
                [fecha] => 2018-08-27 18:36:20
                [id_cliente] => 110099
                [nit] => 3108449-4
                [destino] => Copartes.Gt
                [nombre] => Guillermo Suhr
                [telefono] => 42150465
                [type] => IN
                [mensaje] => Muchas gracias
            )

        [22] => Array
            (
                [fecha] => 2018-08-27 18:30:05
                [id_cliente] => 27523
                [nit] => 1241764-5
                [destino] => Copartes.Gt
                [nombre] => Manuel Garcia
                [telefono] => 53186931
                [type] => IN
                [mensaje] => Gracias
            )

        [23] => Array
            (
                [fecha] => 2018-08-27 18:30:00
                [id_cliente] => 110099
                [nit] => 3108449-4
                [destino] => Copartes.Gt
                [nombre] => Guillermo Suhr
                [telefono] => 42150465
                [type] => IN
                [mensaje] => Gracias
            )

        [24] => Array
            (
                [fecha] => 2018-08-27 18:30:00
                [id_cliente] => 110099
                [nit] => 3108449-4
                [destino] => Copartes.Gt
                [nombre] => Guillermo Suhr
                [telefono] => 42150465
                [type] => IN
                [mensaje] => Buena tarde

Upvotes: 0

Views: 64

Answers (2)

steffen
steffen

Reputation: 16938

Just filter the array:

$a = array_filter($a, function($val) {
    static $set = []; // collect nits
    $nit = $val['nit'];
    if (!isset($set[$nit])) { // new nit
        return $set[$nit] = true; // true
    }
    return false;
});

Explanation: The filter callback is a function that is called for each array element. (Since there are no flags passed to array_filter(), the callback receives the array value only.) This function has a static variable which exists only local in the function scope and doesn't lose it's value when the function call has completed. It collects all nit values so far and returns true if seen for the first time and false else.

Upvotes: 1

Scuzzy
Scuzzy

Reputation: 12332

I always find the easiest thing to do here is a simple foreach loop to sort your items into another array grouped by the key you wish, in my reduced example, I've used category...

// Source Data
$array = array(
  array( 'category' => 10, 'name' => 'aaa' ),
  array( 'category' => 11, 'name' => 'bbb' ),
  array( 'category' => 12, 'name' => 'ccc' ),
  array( 'category' => 10, 'name' => 'ddd' ),
  array( 'category' => 11, 'name' => 'eee' ),
  array( 'category' => 11, 'name' => 'fff' ),
  array( 'category' => 13, 'name' => 'ggg' ),
);

// Create a container using "category"
$groups = array_fill_keys( array_unique( array_column( $array, 'category') ), array() );

// Loop and Sort
foreach( $array as $index => $item )
{
  $groups[ $item['category'] ][ $index ] = $item;
}

// Done
print_r( $groups );

Once you've got this, list you're now able to figure out which have multiple records or not by the count within each sub array.

Upvotes: 0

Related Questions