PHP:如果在多维数组中发现重复项,则合并相邻值 [英] PHP: Merging adjacent values if duplicates found in a multidimensional array

查看:107
本文介绍了PHP:如果在多维数组中发现重复项,则合并相邻值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些PHP变量集,可以用来制作多维数组。现在,在该数组中,我想检查特定键( [font] )是否重复。

I have some PHP variable sets from which I'm making a multidimensional array. Now, in that array, I want to check for a particular key ([font]) for duplicates.

如果发现重复,则 [lang] [weight] 的相应值和相应值应合并。

If duplicates found, the corresponding and respective values for [lang] and [weight] should merge.

这是到目前为止我尝试过的操作(此操作从数组中取消/删除重复值):

Here is what I've tried so far (this unsets/removes the duplicate value from the array):

// Font [0]
$font_1 = "Poppins";
$font_1_l = "Hindi, English";
$font_1_w = "700, 700i";

// Font [1]
$font_2 = "Lora";
$font_2_l = "Vietnamese, Japanese";
$font_2_w = "200, 300, 400, 400i";

// Font [2]
$font_3 = "Noto Sans";
$font_3_l = "Punjabi, Latin, Hindi";
$font_3_w = "200, 200i, 300, 300i, 400, 500";

// Font [3]
$font_4 = "Lora";
$font_4_l = "Greek, Roman, Vietnamese";
$font_4_w = "400, 400i, 500, 500b";

// Array of all the values
$font_f = array( array( 'font' => $font_1, 'lang' => $font_1_l, 'weight' => $font_1_w ), array( 'font' => $font_2, 'lang' => $font_2_l, 'weight' => $font_2_w ), array( 'font' => $font_3, 'lang' => $font_3_l, 'weight' => $font_3_w ), array( 'font' => $font_4, 'lang' => $font_4_l, 'weight' => $font_4_w ) ); 

// Printing the array for testing
echo "<pre>";
print_r( array_map("unserialize", array_unique(array_map("serialize", $font_f))) );

// Removing duplicates
$font_f_copy = $font_f; // Copy of $font_f for modification
$fonts = array(); // To get unique fonts

for( $i=0; $i<count($font_f); $i++ ) {
  if ( in_array( $font_f[$i]['font'], $fonts ) ) {
    unset($font_f_copy[$i]);
  }
  else {
    $fonts[] = $font_f[$i]['font'];
  }
}

// Printing $font_f_copy for testing
print_r($font_f_copy);

输出

Array
(
    [0] => Array
        (
            [font] => Poppins
            [lang] => Hindi, English
            [weight] => 700, 700i
        )

    [1] => Array
        (
            [font] => Lora
            [lang] => Vietnamese, Japanese
            [weight] => 200, 300, 400, 400i
        )

    [2] => Array
        (
            [font] => Noto Sans
            [lang] => Punjabi, Latin, Hindi
            [weight] => 200, 200i, 300, 300i, 400, 500
        )

    [3] => Array
        (
            [font] => Lora
            [lang] => Greek, Roman, Vietnamese
            [weight] => 400, 400i, 500, 500b
        )

)
Array
(
    [0] => Array
        (
            [font] => Poppins
            [lang] => Hindi, English
            [weight] => 700, 700i
        )

    [1] => Array
        (
            [font] => Lora
            [lang] => Vietnamese, Japanese
            [weight] => 200, 300, 400, 400i
        )

    [2] => Array
        (
            [font] => Noto Sans
            [lang] => Punjabi, Latin, Hindi
            [weight] => 200, 200i, 300, 300i, 400, 500
        )

)

您可以参见上面的代码,对于 [font] 即Lora,字体[1]和字体[3]的值相同,因此 [lang ] [weight] 字体[1]应该与 [lang] [weight] 分别表示字体[3]。

As you can see in the code above, Font [1] and Font [3] will have the same value for [font] i.e. Lora, so the [lang] and [weight] for Font [1] should merge with [lang] and [weight] for Font [3] respectively.

我想知道如何实现这一目标。

I'm wondering how to proceed to achieve that.

推荐答案

这是真正的主力军:



从<$ c开始$ c> $ font_f 这将:


  1. 连接每个$ b $中重复字体的lang和weight值b子数组。

  2. 删除lang和weight CSV字符串中的重复值。

  3. 将ASC唯一的lang和weight值排序。

  4. 按其键/格式对子数组进行排序t名称。

  5. 用数字索引替换关联子数组键。

  1. Concatenate lang and weight values on duplicate fonts in each subarray.
  2. Remove duplicate values in the lang and weight CSV strings.
  3. Sort ASC the unique lang and weight values.
  4. Sort the subarrays by their key / font name.
  5. Replace the associative subarray keys with numeric indices.

这里是代码:

$array=[];
foreach($font_f as $a){
    $array[$a["font"]]["font"]=$a["font"];                      // declare or lazy overwrite if duplicate
    foreach(array("lang","weight") as $col){                    // cycle through the concat-able elements
        if(isset($array[$a["font"]][$col])){
            $temp_str=$array[$a["font"]][$col].", ".$a[$col];   // concat
        }else{
            $temp_str=$a[$col];                                 // declare
        }
        $temp_arr=array_unique(explode(', ',$temp_str));        // split & remove duplicates
        sort($temp_arr);                                        // sort
        $array[$a["font"]][$col]=implode(', ',$temp_arr);       // rejoin
    }
}
ksort($array);                    // sort subarrays by font name
$array=array_values($array);      // replace temporary associative keys with indices

echo "<pre>";
print_r($array);
echo "</pre>";

输出:

Array(
    [0] => Array(
        [font] => Lora
        [lang] => Greek, Japanese, Roman, Vietnamese
        [weight] => 200, 300, 400, 400i, 500, 500b
    ),    
    [1] => Array(
        [font] => Noto Sans
        [lang] => Hindi, Latin, Punjabi
        [weight] => 200, 200i, 300, 300i, 400, 500
    ),
    [2] => Array(
        [font] => Poppins
        [lang] => English, Hindi
        [weight] => 700, 700i
    )
)

这篇关于PHP:如果在多维数组中发现重复项,则合并相邻值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆