删除重复元素数组php

时间:2018-02-26 18:56:06

标签: php arrays laravel-5

我的控制器中有这段代码

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>";
}  

这是数组的结果

  

数组([IVA] =&gt; 0)

     

数组([IVA] =&gt; 0)

     

数组([IVA] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 123 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 1111111 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 7920 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 7920 [ISR] =&gt; 0)

     

数组([IVA] =&gt; 0 [IEPS] =&gt; 14174 [ISR] =&gt; 0)

如何删除重复的元素?

3 个答案:

答案 0 :(得分:0)

您可以尝试使用

$array_result =   array_unique($total_traslados, SORT_REGULAR);

var_dump($array_result);

或尝试=

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

var_dump($total_traslados);

答案 1 :(得分:0)

目前,您只是在每个循环中打印结果。如果你不想在整个循环期间重复,你不应该在循环期间但在之后回显。

尝试类似:

$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);

编辑更紧凑的解决方案

答案 2 :(得分:0)

我没有测试过这个,但这应该有效:

// 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 );
}