
Originally Posted by
_matrix_
Hi,
When using:
PHP Code:
foreach($_POST['total'] as $total) {
how can you grab only records where $_POST['total'] contain data?
Are you looking to only get the elements of $_POST['total'] that have data in them? Or only loop through $_POST['total'] if it has data in it?
If you only want to use elements in $_POST['total'] that have values, you've got a couple options. You can't selectively choose which elements to iterate with a foreach, so you'll need to either add some logic to the loop or filter the array beforehand.
The simplest thing would be to include an if statement in the loop.
PHP Code:
foreach ($_POST['total'] as $total) {
if ($total) {
// $total has a value, continue processing it
}
}
Or, you could use the array_filter function. It allows you to pass all of the array values through a callback function and then throws them away if they fail the test. Your callback function could simply check to make sure that the element has a filled value.
PHP Code:
function notEmpty($var) {
return !(empty($var));
}
$filteredTotals = array_filter($_POST['total'], "notEmpty");
foreach ($filteredTotals as $total) {
// Process $totals here
}
Good luck,
- Walkere
Bookmarks