note 98928 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Fri, 16 Jul 2010 13:03:59 +0000 |
| Subject: | note 98928 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170902@lists.php.net to get a copy of this message | ||
Note Submitter: Dean Moncaster
----
In regard to John's post about editing an array being processed with foreach, i couldn't
get edited array elements to be displayed so i wrote the following code that, after taking the input
from a MySQL database, checks all array elements for an empty value and then replaces the empty
value with static text.
$sqlgetname = "SELECT * FROM
mytable ORDER BY firstname";
$resultgetname = mysql_query($sqlgetname);
while($rowgetname = mysql_fetch_row($resultgetname))
{
$count = 0;
foreach ($rowgetname as $value) {
if ($value == "")
{
$rowgetname[$count] = "Empty String";
}
$count++;
}
$first= $rowgetname[0];
$second = $rowgetname[1];
$third = $rowgetname[2];
$fourth = $rowgetname[3];
$fifth = $rowgetname[4];
$sixth = $rowgetname[5];
$seventh = $rowgetname[6];
$eighth = $rowgetname[7];
echo($first . " " . $second . " " . $third . " " . $fourth . "
" . $fifth . " " . $sixth . " " . seventh . " " . $eighth .
"<br/> ");
}
This should populate an array and replace all elements of the array that are empty with the words
"Empty String" and then assign the array elements to variables which are then echoed to
the screen before going onto the next item in the array.