2012-05-22 124 views
0

我創建了一個銷售各種卡片的簡單訂單。這些卡片有不同的顏色。下面的代碼顯示了提供的顏色。我需要能夠從下面的數組中獲取數據,以便我可以確定用戶在表單上爲了訂購目的而選擇的每種顏色的數量。從數組中提取數據(PHP)

Array 
(
    [qty] => Array 
     (
      ['red'] => 0 
      ['blue'] => 0 
      ['green'] => 0 
      ['yellow'] => 0 
      ['orange'] => 0 
      ['white'] => 0 
      ['black'] => 0 
      ['purple'] => 0 
      ['teal'] => 0 
      ['grey'] => 0 
     ) 

任何幫助,我非常感謝。

+2

你要什麼用數據做什麼?如果您有一個數據數組,並且數量分配給每個索引,那麼這個數據處於可用狀態。請告訴我們你想從那裏去的地方。 –

回答

0

嘗試是這樣的:

<?php 
    $some_input = array('red','red','white'); 
    $a =array('qty' => array(
    'red' => 0, 
    'blue' => 0, 
    'green' => 0, 
    'yellow' => 0, 
    'orange' => 0, 
    'white' => 0, 
    'black' => 0, 
    'purple' => 0, 
    'teal' => 0, 
    'grey' => 0, 
)); 
    $f = function($x) use(&$a) 
    { 
     if(in_array($x, $a['qty'])) 
     { 
      $a['qty'][$x]++; 
     } 
    }; 
    array_map($f, $some_input); 

    var_dump($a); 
?> 

你可以看到運行here點擊按鈕的代碼粘貼

0

你只是想在頁面上顯示的信息?

如果是的話,這應該爲你做這項工作(假設你的數組稱爲$陣列):

<table> 
    <thead> 
     <tr> 
      <th>Colour</th> 
      <th>Qty</th> 
     </tr> 
    </thead> 
    <tbody> 
    <?php foreach($array['qty'] AS $colour => $total) { ?> 
     <tr> 
      <td><?php echo $colour; ?></td> 
      <td><?php echo $total; ?></td> 
     </tr> 
    <?php } ?> 
    </tbody> 
</table>