Filter array value [duplicate]

2

My array will be:

$arr = array();

while($r = mssql_fetch_assoc($query)) {

    $arr = array('cnpj' => $r['cnpj'], 'empresa' => $r['nome']);

}

No while, there are several results (repeated CNPJ), and I would like to assemble the array with only one index for each CNPJ, and not create several indices with the same CPNJ repeated, if there is 5 CNPJ in WHILE, save only once The CNPJ.

I would like to know how to check if the CNPJ already exists in the array before inserting it.

PS: I am generating an array from a while (), I am not wanting to remove duplicate value from an existing array.

    
asked by anonymous 29.11.2017 / 13:43

3 answers

2

With all the data and messages you can not resolve in the , then do so:

$arr = array();

while($r = mssql_fetch_assoc($query)) 
{    
    $b = $r['cnpj'];
    if (count(array_filter($arr, function($a) use($b)  { return $b == $a['cnpj'];}))==0) 
    {
        $arr[] = array('cnpj' => $r['cnpj'], 'empresa' => $r['nome']);    
    }
}

29.11.2017 / 14:01
1

Another way to resolve this problem is to combine in_array() to find out if cnpj is already exists in the array or not. For the comparison to be made correctly use array_column() this function extracts all values of subindice .

$arr = array();
while($r = mssql_fetch_assoc($query)) {
    if(!in_array($r['cnpj'], array_column($arr, 'cnpj'))){
       $arr[] = $r;
    }
}

The original array has more or less this structure:

Array
(
    [0] => Array
        (
            [empresa] => AAA
            [cnpj] => 1
        )

    [1] => Array
        (
            [empresa] => BBB
            [cnpj] => 3
        )
)

With the call array_column($arr, 'cnpj') it is transformed to:

Array
(
    [0] => 1
    [1] => 3
)

Example - repl.it

    
29.11.2017 / 14:24
1

You can also merge array_map and array_unique :

$fromSQL = [
    [
        'empresa' => 'abc',
        'cnpj' => 72905498000142
    ],
    [
        'empresa' => 'abc',
        'cnpj' => 72905498000142
    ],
    [
        'empresa' => 'ghi',
        'cnpj' => 21808437000126
    ],
    [
        'empresa' => 'jkl',
        'cnpj' => 19107168000129
    ],
    [
        'empresa' => 'mno',
        'cnpj' => 65566224000100
    ]
];

You would have the result:

array (size=5)
  0 => 
    array (size=2)
      'empresa' => string 'abc' (length=3)
      'cnpj' => int 72905498000142
  1 => 
    array (size=2)
      'empresa' => string 'abc' (length=3)
      'cnpj' => int 72905498000142
  2 => 
    array (size=2)
      'empresa' => string 'ghi' (length=3)
      'cnpj' => int 21808437000126
  3 => 
    array (size=2)
      'empresa' => string 'jkl' (length=3)
      'cnpj' => int 19107168000129
  4 => 
    array (size=2)
      'empresa' => string 'mno' (length=3)
      'cnpj' => int 65566224000100

Applying map to unique :

$arr = array_map("unserialize", array_unique(array_map("serialize", $fromSQL)));

It would result in:

array (size=4)
  0 => 
    array (size=2)
      'empresa' => string 'abc' (length=3)
      'cnpj' => int 72905498000142
  2 => 
    array (size=2)
      'empresa' => string 'ghi' (length=3)
      'cnpj' => int 21808437000126
  3 => 
    array (size=2)
      'empresa' => string 'jkl' (length=3)
      'cnpj' => int 19107168000129
  4 => 
    array (size=2)
      'empresa' => string 'mno' (length=3)
      'cnpj' => int 65566224000100
    
29.11.2017 / 14:49