-1

I have this code in my controller

foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Impuestos//cfdi:Traslados//cfdi:Traslado') as $col){

    if($col['impuesto']=='IVA'){                                                                                                                                                        
        $total_traslados['IVA']=0;
    }
    if($col['impuesto']=='IEPS'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['IEPS']=$total_iva;
    }
    if($col['impuesto']=='ISR'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['ISR']=0;
    }
    echo "<br>";
    print_r($total_traslados);
    echo "<br>";
}  

And this is the result of the array

Array ( [IVA] => 0 )

Array ( [IVA] => 0 )

Array ( [IVA] => 0 )

Array ( [IVA] => 0 [IEPS] => 123 )

Array ( [IVA] => 0 [IEPS] => 123 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 123 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 123 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 123 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 123 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 1111111 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 7920 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 7920 [ISR] => 0 )

Array ( [IVA] => 0 [IEPS] => 14174 [ISR] => 0 )

How can i delete repeated elements?

Lu_kors
  • 80
  • 6
  • [How to Make Multiple Dimension Arrays Unique](https://stackoverflow.com/questions/4585208/how-can-you-make-a-multidimensional-array-unique) here is a start. Your code right now does what it is meant to do. I think maybe search some more and try stuff or show us how you have attempted to resolve your problem before asking for an answer. – mcv Feb 26 '18 at 19:39

3 Answers3

0

You could try using

$array_result =   array_unique($total_traslados, SORT_REGULAR);

var_dump($array_result);

or try =

$total_traslados = array_map("unserialize", 
             array_unique(array_map("serialize", $total_traslados)));

var_dump($total_traslados);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

At the moment you are only printing the results every loop. If you don't want duplicates during the whole loop you should not echo during the loop but after.

Try something like:

$res = [];
foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Impuestos//cfdi:Traslados//cfdi:Traslado') as $col){

    if($col['impuesto']=='IVA'){                                                                                                                                                        
        $total_traslados['IVA']=0;
    }
    if($col['impuesto']=='IEPS'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['IEPS']=$total_iva;
    }
    if($col['impuesto']=='ISR'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['ISR']=0;
    }
    $res[] = $total_traslados;
}

//due to $res has subarrays you have to use the SORT_REGULAR Flag
$res = array_unique($res,SORT_REGULAR);
var_dump($res);

Edit: more compact solution

rlandster
  • 7,294
  • 14
  • 58
  • 96
Lu_kors
  • 80
  • 6
0

I have not tested this but this should work:

// This will hold only the unique array combinations
$unique_array = array();

foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Impuestos//cfdi:Traslados//cfdi:Traslado') as $col){

    if($col['impuesto']=='IVA'){                                                                                                                                                        
        $total_traslados['IVA']=0;
    }
    if($col['impuesto']=='IEPS'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['IEPS']=$total_iva;
    }
    if($col['impuesto']=='ISR'){
        $total_iva  =   $total_iva + $col['importe'];
        $total_traslados['ISR']=0;
    }

    // We enforce uniqueness by setting the key to the output of print_r
    // This might not be the most efficient idea but it should work
    $unique_array[ print_r($total_traslados, true) ] = $total_traslados;
}

foreach( $unique_array as $v )
{
    print_r( $v );
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77