How do I remove duplicates from a preg_match_all generated array?

白昼怎懂夜的黑 提交于 2019-12-11 09:47:46

问题


How do I remove duplicates from a preg_match_all generated array?

Current array

Array
    (
    Array
        (
            'font-family: "Comic Sans";',
            'font-weight: bold;',
            'font-weight: normal;',
            'font-family: "Comic Sans";',
            'font-weight: normal;'
        )

    Array
        (
            'font-family',
            'font-weight',
            'font-weight',
            'font-family',
            'font-weight'
        )

    Array
        (
            '"Comic Sans"',
            'bold',
            'normal',
            '"Comic Sans"',
            'normal'
        )
    )

As you can see there are several duplicate values. The new array without the duplicate values should look like this.

New array

Array
    (
    Array
        (
            font-family: "Comic Sans",
            font-weight: bold,
            font-weight: normal,
        )

    Array
        (
            font-family,
            font-weight,
            font-weight
        )

    Array
        (
            "Comic Sans",
            bold,
            normal
        )
    )

I know I could do this with an foreach but I`m sure there is a much prettier way to accomplish this result. What do I overlook?


回答1:


You can do it via:

//$rgData comes from preg_match_all
$rgResult = array_map('array_unique', $rgData);



回答2:


try to just use array_unique function

http://www.php.net/manual/en/function.array-unique.php

 $input = array(4, "4", "3", 4, 3, "3");
 $result = array_unique($input);
 var_dump($result);

output will be

 array(2) {
   [0] => int(4)
   [2] => string(1) "3"
 }



回答3:


Use:

<?php
foreach($array as $key=>$each) {
    $array[$key] = array_unique($each);
}
print_r($array);
?>



回答4:


try this :

foreach($yourArray as $array){
  $array = array_unique($array);

}


来源:https://stackoverflow.com/questions/18867142/how-do-i-remove-duplicates-from-a-preg-match-all-generated-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!