dongmeng1868 2016-07-08 11:05
浏览 71
已采纳

PHP在递归数组中移动重复项

I building a translation form that is build using a array filled with the translation keys, to keys are however often duplicated. As the form is builded in group i want to move the duplicates to the $translatables['global'] Note that what you're seeing in the image below is already placed in $translatables['modules']['Module_{{ModuleName}}']

As you can see in the image below, some fields are duplicates. those needs to be moved. The duplicates can be placed accross multiple elements. If the issue isn't clear you can see it here

I edit for the duplicate message: It ain't, its a question to move those values not to filter them (and keep 1 remaining), they need to be placed under a global section.

  • 写回答

1条回答 默认 最新

  • douyasihefu6214 2016-07-08 13:06
    关注

    I solved it myself

    class Tools_Array
    {
        public static function find_duplicates_recursive(&$array, $remove = false)
        {
            $duplicates = array();
    
            $valueCounts = array_count_values(self::array_values_recursive($array));
            foreach ($valueCounts as $key => $value) {
                if ($value > 1)
                {
                    $duplicates[] = $key;
                }
            }
    
            if ($remove) {
                foreach ($duplicates as $duplicate)
                {
                    $array = self::remove_values_recursive($array, $duplicate);
                }
            }
    
            return $duplicates;
        }
    
        public static function array_values_recursive($array)
        {
            $arr2 = array();
            foreach ($array as $key => $value)
            {
                if(is_array($value))
                {
                    $arr2 = array_merge(array_values_recursive($value), $arr2);
                }else{
                    $arr2[] = $value;
                }
            }
    
            return $arr2;
        }
    
        public static function remove_values_recursive($array, $needle)
        {
            foreach ($array as $key => $value)
            {
                if(is_array($value))
                {
                    $array[$key] = self::remove_values_recursive($value, $needle);
                }else{
                    if ($value == $needle)
                    {
                        unset($array[$key]);
                    }
                }
            }
    
            return $array;
        }
    }
    

    Then i can filter it with:

    $duplicates = Tools_Array::find_duplicates_recursive($translatables, true);
    $translatables['globaal'] = array_merge($translatables['globaal'], $duplicates);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题