如何从数据库中显示此维数组中的所有值,而不显示重复值?
以下是示例代码。
$darray = Array
(
Array ( [0] => apple, [1] => pear, [2] => appetiser )
Array ( [0] => apple, [1] => pear, [2] => appetiser )
Array ( [0] => indian, [1] => vegetarian, [2] => entree )
Array ( [0] => german, [1] => snack, [2] => vegetarian )
Array ( [0] => mexican, [1] => soup, [2] => pear )
Array ( [0] => apple, [1] => snack )
Array ( [0] => italian, [1] => entree, [2] => pear, [3] => vegetarian )
)
答案 0 :(得分:0)
循环遍历多维数组,收集值并使用array_unique()提取唯一值:
<?php
$darray = array
(
array('apple', 'pear', 'appetiser'),
array('apple', 'pear', 'appetiser'),
array('indian', 'vegetarian', 'entree',),
array('german', 'snack', 'vegetarian') ,
array('mexican', 'soup', 'pear'),
array('apple', 'snack'),
array('italian', 'entree', 'pear', 'vegetarian'),
);
$array = array();
foreach ($darray as $monodimensional_array)
{
foreach ($monodimensional_array as $value)
{
$array[] = $value;
}
}
$array = array_unique($array);
答案 1 :(得分:0)
您还可以使用中间存储阵列来存储已打印的值并进行查询,以确保只打印唯一的数组值。
<?php
$darray = array
(
array ( 0 => 'apple', 1 => 'pear', 2 => 'appetiser' ),
array ( 0 => 'apple', 1 => 'pear', 2 => 'appetiser' ),
array ( 0 => 'indian', 1 => 'vegetarian', 2 => 'entree' ),
array ( 0 => 'german', 1 => 'snack', 2 => 'vegetarian' ) ,
array ( 0 => 'mexican', 1 => 'soup', 2 => 'pear' ),
array ( 0 => 'apple', 1 => 'snack' ),
array ( 0 => 'italian', 1 => 'entree', 2 => 'pear', 3 => 'vegetarian' )
);
$printedArray = array();
foreach($darray as $darr)
{
foreach($darr as $val)
{
if(!in_array($val,$printedArray))
{
echo $val."<br>";
//you could alternately store the value into an array[] here
//and print it later...
array_push($printedArray,$val);
}
}
}
?>
这应该打印:
apple
pear
appetiser
indian
vegetarian
entree
german
snack
mexican
soup
italian