note 98928 added to control-structures.foreach
| From: | DeanMoncaster at osu1 dot php dot net | Date: | Fri, 16 Jul 2010 09:09:35 +0000 |
| Subject: | note 98928 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170895@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 82.109.45.66
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/98928
Del: integrated -- https://master.php.net/note/delete/98928/integrated
Del: useless -- https://master.php.net/note/delete/98928/useless
Del: bad code -- https://master.php.net/note/delete/98928/bad+code
Del: spam -- https://master.php.net/note/delete/98928/spam
Del: non-english -- https://master.php.net/note/delete/98928/non-english
Del: in docs -- https://master.php.net/note/delete/98928/in+docs
Del: other reasons-- https://master.php.net/note/delete/98928
Reject -- https://master.php.net/note/reject/98928
Search -- https://master.php.net/manage/user-notes.php