Combine two foreach loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nightmaster
Forum Newbie
Posts: 5
Joined: Sun Nov 15, 2015 12:21 pm

Combine two foreach loops

Post by Nightmaster »

Hey there.
I need to display data from two foreach loops together through a table or something. I have a code:

Code: Select all

  $blic_xpath = new DOMXPath($blic_doc);

  // getting h4 elements
  $podaci_levo = $blic_xpath->query('//h4');
      foreach($podaci_levo as $stubic){
		  echo $stubic->nodeValue; 
	  }
      
	    // getting comments from div
  $komentari = $blic_xpath->query('//div[@class="inner"]');
      foreach($komentari as $kom){
		  echo $kom->nodeValue;
}
This will output data like this:

Code: Select all

value1 value2 value3 comment1 comment2 comment3
And i'd like something like:

Code: Select all

value1 comment1
value2 comment2
value3 comment3
...
I tried few things, but whatever I do looks messy and didn't work well. Any hints please? :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Combine two foreach loops

Post by requinix »

What is the HTML markup you're working with? Because it will probably be better to loop over some common ancestor and pick the single h4 and div inside each.
Post Reply