I have created a simple order form that is selling a variety of cards. These cards come in different colors. The code below shows the colors offered. I need to be able to pull the data from the array below so that I can determine how many of each color did the user select on the form for ordering purposes.
Array
(
[qty] => Array
(
['red'] => 0
['blue'] => 0
['green'] => 0
['yellow'] => 0
['orange'] => 0
['white'] => 0
['black'] => 0
['purple'] => 0
['teal'] => 0
['grey'] => 0
)
Any assistance with this I greatly appreciate.
Try something like this:
<?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);
?>
You can see the code running here click in the button paste
Do you just want to display the info on a page?
If so, this should do the job for you (assuming your array is called $array):
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.