Filter array value

Asked

Viewed 232 times

2

My array will be:

$arr = array();

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

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

}

In while, there are several results (repeated CNPJ), and I would like to mount the array with only one Dice for each CNPJ, and not create several indices with the same repeated CPNJ, 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(), not wanting to remove duplicate value from an existing array.

  • It’s value, not index, I edited the question.

  • Add more information and it’s not clear what you need to do. I understand you try to compare the data but I don’t understand the logic, please explain better so we can help.

  • this variable $arr would be to save the array? ie, would not be $arr[] ?

  • 1

    Resolve this in your SQL !!!

  • It’s a third-party trial, I’m implementing PHP only, and I don’t have access to the trial, I just get the list of procedures.

  • I disagree, what I need is to generate an array, not remove duplicate from an existing array.

Show 1 more comment

3 answers

3


With all the data and messages that we can not solve in , 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']);    
    }
}

References

  • @Eliseub. I changed the function... take a look now

  • It worked perfectly. Grateful!

1

Another way to solve this problem is to combine in_array() to know whether cnpj already exists in the array or not. To make the comparison correctly use array_column() this function extracts all subindice values.

$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') he is transformed to:

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

Example - repl.it

1

You can also merge the functions 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 the map with unique:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.