Taking user input and inserting it into an associative array using PHP -
i need put user input associative array. program asks user how many people in group. asks each person's name along order. proceeds print out group member's names along orders. idea use for
loop add each data set array. think have figured out except how put data array. here code far:
<?php $people=readline("how many orders?"); $orders=[]; for($i=0; $i<$people; $i++) { print("order "); print($i); print(" name: "); $name=readline(); print("order "); print($i); print(" order: "); $food=readline(); } print("total order:\n"); foreach($orders $names=>$orders) { print($names); print(": "); print($orders); print("\n"); } ?>
php has of best documentation of programming language, advise taking peak @ array
intro, show need know.
as tip, because you're going keeping track of multiple datums per order (name, food), , want able associate these each other later, adding arrays $orders
array. example:
$orders[$i] = [$name, $food];
this can accessed later using numeric indexing:
$orders[$i][0] // value of $name order $i $orders[$i][1] // value of $food order $i
but, in case, advise taking next step , using associative arrays instead:
$orders[$i] = ['name' => $name, 'food' => $food]
this makes access easier remember , read:
$orders[$i]['name'] // value of $name order $i $orders[$i]['food'] // value of $food order $i
Comments
Post a Comment