0

这是我的示例数组

    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>';

这是我想展示的..

4

5 回答 5

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);
于 2012-10-16T06:37:10.917 回答
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>';
于 2012-10-16T06:40:33.490 回答
1

试试这个

$unique_colors = array_reduce(
  $your_arr, 
  function($res, $elem) { 
    if (!in_array($elem['color'], $res)) {
      $res[] = $elem['color'];
    }
    return $res; 
  }, 
  array()
);
于 2012-10-16T06:45:24.453 回答
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;
}
于 2012-10-16T06:46:56.447 回答
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>';

?>
于 2012-10-16T06:40:32.850 回答