• t
  • g
  • B
  • Z
  • @
  • e

portfolio & blog of senior web developer, Fahd Murtaza



  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

PHP: for loop or foreach loop with mutiple arrays. Multiple arrays in a single foreach loop

December 6th, 2007 by Fahd

Here is an example for toy to understand the concept of multiple arrays in a single loop in PHP. My friend and coworker Mohammad Khan (yeah he is from Peshawar and sits beside me in the office where we develop web applications) was working on a such a situation for an airline scheduling system for Intercontinental Airways with HTML form submission in PHP where he has multiple arrays passed as POST variables. So I did a quick search on internet and found solutions spread across the forums. I decided to do the quick and dirty mash up of all the stuff with proper editing to get you guys going on the same path along!

Lets suppose you have two arrays one for links and other for the link name. You want to us a single foreach to mash them up. Have a look

PHP Code:

$href = array('htmlfile.html', 'giffile.gif', 'phpfile.php', 'thejpglink.jpg');
$name = array('My html file', 'My gif file', 'My php file', 'My jpeg file');

You might be thinking, “How do I make it so that I can combine both arrays to make a link?”. Yeah thats what I am gonna teach you guys. Oh I forgot to mention the gals ;) . Yep ladies I heard you saying, “I want the output to be:

 

PHP Code:

<a href="htmlfile.html">My html file</a>
<
a href="giffile.gif">My gif file</a>
<
a href="phpfile.php">My php file</a>
<
a href="thejpglink.jpg">My jpg file</a>

. And I want to do this with a single foreach”.

Thoughts: Do you still think For only works with one array? Yeah you are wrong!

In both, assume that $ar1 is the first array, and $ar2 is the second array.

echo $ar1[$n].$ar2[$n]; means echo the value of that part of the first array, then that part of the second array. You could place them into links as you wish.

 

PHP Code:

for ($n=0; isset($ar1[$n]) && isset($ar2[$n]; $n++) { //for loop:
//set $n to zero first time
//while both the $ar1 and $ar2 at $n are set, like $ar2[7], etc.
echo $ar1[$n].$ar2[$n];
}

Posted in Open Source, PHP, PHP Tips, Web Development

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories


Want to Hire Me?


Fahd Murtaza is the guy behind fahdmurtaza.com: your source for getting your web development projects done. Fahd sepcialises in Open Source Web Development and his favorite is WordPress. This site is a platform to his web development portfolio and blog all on a newly designed responsive website—try viewing it on your mobile.

If after viewing Fahd's portfolio, you think he might be just the guy to hire for your web / application development; check out the web development pricing page, then find out how to hire Fahd with a web development project idea and a brief of requirements.

 


Fahd Murtaza has put together a number of Web Development Articles that will help you see what he does, how he does it. You will also be able to see the level of work, and passion, that Fahad puts into each and every job. Understanding a little about the process can make the idea of investing good money much more palatable. People have always appreciated my Agile techniques. Its time you

Fahd Murtaza, Web Developer, Programmer, Wordpress Expert
U
This is portfolio & blog of senior web developer Fahd Murtaza, who has 9 years experience in: website development, WordPress, drupal, CMS and CRM application development with passion of making web better; one site at a time.


@
Mobile +968 93 678 199
email info@fahdmurtaza.com
Google Talk: fahdim@gmail.com
Skype fahd.murtaza
Location Muscat, Oman.


_
Developed using my beloved, love of my life, WordPress, built on the responsive, grid based, mobile optimised, Foundation Framework, and a modified Foundation theme. More Info →


Follow Fahd: Twitter / Google+ / Instagram / Facebook / Dribbble / Tumblr / Posterous/ flickr /
Copyright © 2006-2012 Fahd Murtaza

    • WordPress.org
    • Documentation
    • Support Forums
    • Feedback