PHP:如果在多维数组中找到重复项,则合并相邻值

时间:2017-03-21 13:46:05

标签: php arrays multidimensional-array

我有一些PHP变量集,我正在从中创建一个多维数组。现在,在该数组中,我想检查重复项的特定键([font])。

如果找到重复项,则[lang][weight]的相应值和相应值应合并。

这是我到目前为止所尝试的内容(这会取消/删除数组中的重复值):

// 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 [1]和Font [3]将具有[font]即Lora的相同值,因此Font的[lang][weight] [1]应分别与{[1}}和[lang]合并为Font [3]。

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

2 个答案:

答案 0 :(得分:1)

而不是for循环,我个人会做类似的事情:

$result = [];
foreach ($font_f as $f) {
     if (isset($result[$f["font"]])) {
         $result[$f["font"]] = [ 
               "font" => $f["font"], 
               "lang" => $f["lang"]." ,".$result[$f["font"]]["lang"],
               "weight" => $f["weight"]." ,".$result[$f["font"]]["weight"]
         ]; 
     } else {
        $result[$f["font"]] = $f;
     }
}

答案 1 :(得分:1)

这是一个真正的主力:

$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>"; 开始,这将:

  1. 在每个中的重复字体上连接lang和weight值 子阵列。
  2. 删除lang和weight CSV字符串中的重复值。
  3. 为ASC排序唯一的郎和重量值。
  4. 按子键/字体名称对子阵列进行排序。
  5. 用数字索引替换关联子阵列键。
  6. 以下是代码:

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

    输出:

    {{1}}