问题
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