PHP: Remove duplicates ARRAY [duplicated]

0

I have armed an array of this type:

$array =array('epc' => "$tag",);

When doing a print of $ array I get the following:

  

Array ([epc] => 1792) Array ([epc] = > 1792) Array ([epc] => 1212) Array ([epc] = > 1792) Array ([epc] = & gt ; 1792) Array ([epc] => 1212) Array ([epc] = > 1792) Array ([epc] => 1212) Array ([epc] = > 1792) Array ([epc] = & gt ; 1792) Array ([epc] = > 1792) Array ([epc] = > 1792) Array ([epc] = > 1792) Array ([epc] = > 1792) Array ([epc] = & gt ; 1212) Array ([epc] = > 1792) Array ([epc] = > 1792)

I am looking to obtain as a result only the different values without repeating that is: 1792 and 1212

I have tried using methods such as array_unique, serialize, foreach and I have not obtained results, I always see all the results.

The value I get from reading a text line of a TXT.

$linea = fgets($fp1); 
$campo = explode(" ", $linea); 
$tag = $campo[1]; 
$tag = str_replace('"','',$tag); 
$tag =(string)(int)$tag;
    
asked by Jona Gregov 28.09.2017 в 06:27
source

2 answers

2

It does not seem like a good idea to transport a bunch of data and then discard it, especially if you're working with a bulky array. That is, if you have to send the array to another site and then delete the duplicates ... it's not a good idea.

You can create a completely clean array at the source and then send it wherever you want.

I particularly do not like the messy way in which array_unique builds the new array.

This is a possibility.

I imagined that your line is like this:

"1792" "1792" "1212" "1792"

We are going to clean it, removing the "

How do you want integer values, we will apply this:

$arrEnteros = array_map('intval', explode(' ', $strLimpia));

We will use a strategy with array_count_values which creates an array of occurrences of each value. (It would be like doing the array_unique function.

$arrUnicos = array_count_values($arrEnteros);

And we will create our final array, totally clean and tidy , with the keys epc and as value the old keys of $arrUnicos .

Code

VIEW DEMO     

$linea = '"1792" "1792" "1212" "1792"';
$strLimpia=str_replace('"','',$linea);
$arrEnteros = array_map('intval', explode(' ', $strLimpia));
$arrUnicos = array_count_values($arrEnteros);
$arrFinal=array();
foreach ($arrUnicos  as $key => $value)  {
    $arrFinal[]=array("epc"=>$key);
}

print_r($arrFinal);
echo "VIENDO EL var_dump()";
var_dump($arrFinal);
?>

Result

The array would look like this:

  Array
(
    [0] => Array
        (
            [epc] => 1792
        )

    [1] => Array
        (
            [epc] => 1212
        )

)

With the var_dump we can see that it is created with integer data types:

VIENDO EL var_dump()

array(2) {
  [0]=>
  array(1) {
    ["epc"]=>
    int(1792)
  }
  [1]=>
  array(1) {
    ["epc"]=>
    int(1212)
  }
}
    
answered by 28.09.2017 / 12:10
source
1

You could do something like this:

array_unique - Remove duplicate values from an array

$array = array('epc' => "$tag",);
$resultado = array_unique($array);
print_r($resultado);
    
answered by 28.09.2017 в 07:21