这是我的示例数组
Array
(
[0] => Array
(
[size] => 7.5 D(M) US
[price] => $49.99
[color] => Black Grey
)
[1] => Array
(
[size] => 8 D(M) US
[price] => $46.90
[color] => Black
)
[2] => Array
(
[size] => 8 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[3] => Array
(
[size] => 8.5 D(M) US
[price] => $46.90
[color] => Black Grey
)
[4] => Array
(
[size] => 8.5 4E US
[price] => $49.99
[color] => BLACK/MTLC DARK GREY/BLACK
)
[5] => Array
(
[size] => 9 D(M) US
[price] => $49.99
[color] => Black
)
[6] => Array
(
[size] => 9 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[7] => Array
(
[size] => 9.5 D(M) US
[price] => $49.99
[color] => Black Grey
)
[8] => Array
(
[size] => 9.5 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[9] => Array
(
[size] => 10 D(M) US
[price] => $49.99
[color] => Black
)
[10] => Array
(
[size] => 10 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[11] => Array
(
[size] => 10.5 D(M) US
[price] => $49.99
[color] => Black Grey
)
[12] => Array
(
[size] => 10.5 4E US
[price] => $49.99
[color] => BLACK/MTLC DARK GREY/BLACK
)
[13] => Array
(
[size] => 11 D(M) US
[price] => $54.90
[color] => Black
)
[14] => Array
(
[size] => 11 D(M) US
[price] => $57.99
[color] => Black/Metallic Dark Grey/Black
)
[15] => Array
(
[size] => 11 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[16] => Array
(
[size] => 11.5 D(M) US
[price] => $47.84
[color] => Black Grey
)
[17] => Array
(
[size] => 12 D(M) US
[price] => $50.90
[color] => Black
)
[18] => Array
(
[size] => 12 4E US
[price] => $75.00
[color] => Black/Metallic Dark Grey/Black
)
[19] => Array
(
[size] => 13 D(M) US
[price] => $58.59
[color] => Black
)
[20] => Array
(
[size] => 13 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[21] => Array
(
[size] => 14 D(M) US
[price] => $54.99
[color] => Black
)
[22] => Array
(
[size] => 14 4E US
[price] => $49.99
[color] => Black/Metallic Dark Grey/Black
)
[23] => Array
(
[size] => 15 D(M) US
[price] => $54.99
[color] => Black
)
)
我想要发生的只是获得"color"
echo '<select>';
foreach($store as $item){
echo '<option>'.$item['color'].'</option>';
}
echo '</select>';
这是我要展示它..
答案 0 :(得分:2)
<?php
$items = Array
(
Array
(
'size' => '7.5 D(M) US',
'price' => '$49.99',
'color' => 'Black Grey',
),
Array
(
'size' => '8 D(M) US',
'price' => '$46.90',
'color' => 'Black'
)
);
$colors = array();
foreach ($items as $item) {
$colors[] = $item['color'];
}
$colors = array_unique($colors);
var_dump($colors);
答案 1 :(得分:1)
我知道它不是最好的,但你可以使用它。通过这样做,您可以避免多个循环。
echo '<select>';
$arr = array()
foreach($store as $item){
if(!in_array($item['color'],$arr)){
echo '<option>'.$item['color'].'</option>';
}
array_push($arr, $item['color']);
}
echo '</select>';
答案 2 :(得分:1)
试试这个
$unique_colors = array_reduce(
$your_arr,
function($res, $elem) {
if (!in_array($elem['color'], $res)) {
$res[] = $elem['color'];
}
return $res;
},
array()
);
答案 3 :(得分:1)
尝试使用这个小解决方案:
$Row = Array
(
[0] => Array
(
[size] => 7.5 D(M) US
[price] => $49.99
[color] => Black Grey
)
[1] => Array
(
[size] => 8 D(M) US
[price] => $46.90
[color] => Black
)
);
$color = array();
foreach ($Row as $key => $value) {
$Color = $value['color'];
if(!in_array($Color, $color)) $color[] = $Color;
}
答案 4 :(得分:0)
我做了一个简单的例子
$color=array();
$color2=array();
foreach($store as $item){
array_push($color,$item['color']);
}
$color2=array_unique($color);
echo '<select>';
foreach($color2 as $col){
echo '<option>'.$col.'</option>';
}
echo '</select>';
?>