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 に答える