【问题标题】:delete repeated elements array php删除重复元素数组php
【发布时间】:2018-02-26 18:56:06
【问题描述】:

我的控制器中有这段代码

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] => 0)

数组([IVA] => 0)

数组([IVA] => 0)

数组([IVA] => 0 [IEPS] => 123)

数组([IVA] => 0 [IEPS] => 123 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 123 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 123 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 123 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 123 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 1111111 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 7920 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 7920 [ISR] => 0)

数组([IVA] => 0 [IEPS] => 14174 [ISR] => 0)

如何删除重复的元素?

【问题讨论】:

  • How to Make Multiple Dimension Arrays Unique 这是一个开始。您的代码现在完成了它应该做的事情。我想也许可以搜索更多内容并尝试一些东西,或者在寻求答案之前向我们展示您是如何尝试解决问题的。

标签: php arrays laravel-5


【解决方案1】:

你可以试试

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

【讨论】:

  • 感谢您的回答,但结果相同,它继续显示相同的重复值
  • 用另一种方法更新答案.. 告诉我
【解决方案2】:

我没有对此进行测试,但这应该可以工作:

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

【讨论】:

    【解决方案3】:

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

    尝试类似:

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

    编辑:更紧凑的解决方案

    【讨论】:

    • 调用未定义函数 App\Http\Controllers\var_dum()
    • 你拼错了var_dump() 吗?
    猜你喜欢
    • 2011-03-22
    • 2012-12-01
    • 2014-01-18
    • 2017-10-19
    • 1970-01-01
    • 2018-02-14
    • 2013-05-20
    相关资源
    最近更新 更多