Reputation: 457
I have a form that gives me multiple arrays when submit. My question is if there is a smart way to echo out (to show the user) all the arrays instead of doing a foreach for each array?
Could a solution be to do a function with a foreach loop?
HTML from the TASKS input:
<td><input type="checkbox" name="tasks[<?php echo($value[0]);?>]"value=<?php echo($key[0]);?>></td>
My PHP script:
if(isset($_POST['submit'])) {
$tasks = $_POST['tasks'];
$user = $_POST['user'];
$pickup_at = $_POST['pickup_at'];
$message = $_POST['message'];
$price = $_POST['price'];
$part_missing = $_POST['part_missing'];
Foreach ex. on the TASKS array
foreach($tasks as $key => $keys)
{
echo $key ."<br>";
}
Upvotes: 2
Views: 1819
Reputation: 1010
I think there is problem with your form. Your html sholud be this:
<td><input type="checkbox" name="tasks[]" value="<?php echo($value[0]);?>"></td>
Insted of this:
<td><input type="checkbox" name="tasks[<?php echo($value[0]);?>]"value=<?php echo($key[0]);?>></td>
After that you can print the values like this:
echo implode(',',$tasks);
Upvotes: 0
Reputation: 72256
Instead of
foreach($tasks as $key => $keys) {
echo $key ."<br>";
}
you can do:
echo(implode('<br>', $tasks));
This puts <br>
between the elements of $task
and produces a string. Depending on the HTML context where you echo
the string you may need or may need not to append an extra <br>
after the last element.
Upvotes: 0
Reputation: 11588
You will need to do some kind of iteration - foreach
being the most obvious. You can also use other native functions like array_walk
:
array_walk($array, function($val, $key) {
echo $key, PHP_EOL;
});
But it does't really add anything here. I'd stick with foreach
or var_dump
.
Upvotes: -1
Reputation: 9782
foreach($tasks as $key => $keys)
{
echo $key ."<br>";
}
If its just use to add BR in keys another ways is
// will give you same output that foreach() gives you.
echo implode("<br>", array_keys($tasks));
Upvotes: 0
Reputation: 781706
All the arrays should have indexes in parallel. You only need to use the indexes from one of them, and can then use that as indexes into all the rest:
foreach ($tasks as $i => $task) {
echo "Task $i: $task<br>";
echo " User: {$user[$i]}<br>";
echo " Pickup at: {$pickup_at[$i]}<br>";
...
}
Upvotes: 3