is it possible to have more than 1 array on a foreach loop?
foreach loop
Collapse
X
-
Tags: None
-
-
how would i use the while loop to add the extra array to a foreach loop?
at 1st i thought id have to to the array_combine.. ...that way i can make an extra key and value using the 2 arrays i need. But i still can't figure how to get the extra item into the foreach....once i combined my arrays i tried this
$details = array_combine($ _POST['photoT'], $_POST['captionT']);
// Loop through the POST items
foreach ( (($_POST['picT'] as $picT => $picStart)) && ($details as $photoName => $picCaption)) )
{
$nameCapsA = $xmlobj->addChild($picS tart);
$nameCapsA->addAttribute(" name", $photoName);
$nameCapsA->addAttribute(" caption", $picCaption);
}Comment
-
You can only use 1 array as a foreach() loop's argument.
Am I right in thinking the amount of files you upload will be the same as the amount of captions you have?
The reason I suggested you do it all in one loop, is because (if you are having the same number of items -- above) you could use the array key given from the foreach loop to access the same index of a different array.
Consider this:
Code:$array_1 = array ( 1, 2, 3, 4, 5, 6 ); $array_2 = array ( "one", "two", "three", "four", "five", "six" ); foreach ( $array_1 as $key => $value ) { echo "{$value} = {$array_2[$key]}<br />"; } // Produces: // 1 = one // 2 = two // ... etc.Comment
-
yes you are right in suggesting that but as i have three arrays it's not that easy......ahhhh h but?
what if i have a while loop to generate one and and the use a foreach loop within the while loop for the $key & $value...would that work better? I will do a test and post my code hereComment
-
also please bare in mind this is being used towards simpleXML.....i t doesn't always take php coding as it should once output.......su ch a painComment
-
the form fields are generated with php, below is what is generated. I'm trying to make an array that can be used within the foreach....i dont really know how many arrays I need but i have 3 picT, photoT, captionT.....i' ve named the fields picT[] photoT[] and captionT[] as to use them as an array when i generate the xml file
code....
Code:echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>'; echo '<center><p><input type="hidden" name="picT[]" value="pic'.$pn++.'"/></p></center>'; echo '<center><p><input type="hidden" name="photoT[]" value="PHOTO '.$pto++.'"/></p></center>'; echo '<center><p><input type="text" name="captionT[]" value=""/></p></center>'; echo "<center><p><a href=imgEdit.php?img=$img_path> > Edit Image < </a></p></center><br></td>";Comment
-
for some reason simpleXML wouldn't read the values that were brought in normally through post thus why i've had to use the foreach loop......
if i can't have more than 1 array within the foreach can i combine the 3 so i can use the within the loop?Comment
-
Like I said before, you can loop through one array using foreach() loop, and then use the key given to you, to access the same index in the other arrays.
Code:foreach ( $_POST['picT'] as $key => $value ) { // use $key to access the other elements. echo $value . "<br />"; echo $_POST['photoT'][$key] . "<br />"; echo $_POST['captionT'][$key] . "<br />"; // Do something with your xml object $child = $xml->addChild( $value ); $child->addAttribute( 'name', $_POST['photoT'][$key] ); $child->addAttribute( 'caption', $_POST['captionT'][key] ); }Comment
-
ahhhhhh i definately see what you are saying now markus.......th anks for that I am going to test this, write it down and practice it so i understand it more.
Thanks againComment
-
here is the result......
pic1
PHOTO 1
test01
Notice: Undefined variable: xml in /home/vecre0/public_html/test/8/xmltest.php on line 21
Fatal error: Call to a member function addChild() on a non-object in /home/vecre0/public_html/test/8/xmltest.php on line 21Comment
Comment