Page 1 of 1

Combine two foreach loops

Posted: Sun Nov 15, 2015 12:26 pm
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? :)

Re: Combine two foreach loops

Posted: Sun Nov 15, 2015 1:53 pm
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.