foreach loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    foreach loop

    is it possible to have more than 1 array on a foreach loop?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    What do you mean? Post a sample code so we can see what you're trying to do.

    Comment

    • zabsmarty
      New Member
      • Feb 2007
      • 25

      #3
      yes, by adding array from while loop

      Comment

      • zabsmarty
        New Member
        • Feb 2007
        • 25

        #4
        Try to use while loop

        Try to use while loop for getting array

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #5
          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

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            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

            • anfetienne
              Contributor
              • Feb 2009
              • 424

              #7
              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 here

              Comment

              • anfetienne
                Contributor
                • Feb 2009
                • 424

                #8
                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 pain

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Do you mean you have a multi-dimensional array, or 3 seperate arrays?

                  Can I please see your upload form; I can't make sense of what's happening here.

                  Comment

                  • anfetienne
                    Contributor
                    • Feb 2009
                    • 424

                    #10
                    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

                    • anfetienne
                      Contributor
                      • Feb 2009
                      • 424

                      #11
                      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

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        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

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #13
                          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 again

                          Comment

                          • anfetienne
                            Contributor
                            • Feb 2009
                            • 424

                            #14
                            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 21

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Yeah, you're going to have to use your xml object - the one you've shown in previous threads.

                              Comment

                              Working...