/    Sign up×
Community /Pin to ProfileBookmark

Two foreach loops

Heya there!

I’m trying to “combine” and use this two foreach loops at once, and I lost any idea how to do that.

[CODE] $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 with class “inner”
$komentari = $blic_xpath->query(‘//div[@class=”inner”]’);
foreach($komentari as $kom){
echo $kom->nodeValue;
}[/CODE]

This will output data like this:

[CODE]
value1 value2 value 3 comment1 comment2 comment3
[/CODE]

I would like it to be like:

[CODE]
value 1 comment1
value 2 comment 2
value 3 comment 3

[/CODE]

I tried multiple combinations to make a table but whatever i try i always get a messy result. ?

Any help would be appreciated, as I’m a php beginner. Thanks!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 15.2015 — One possibility (untested):
[code=php]
$podaci_levo = $blic_xpath->query('//h4');
$komentari = $blic_xpath->query('//div[@class="inner"]');
foreach($podaci_levo as $stubic){
$kom = current($komentari);
if($kom === false) {
break; // or however else you want to handle a mismatch
}
next($komentari);
echo "<p>{$stubic->nodeValue} {$kom->nodValue}</p>n";
}
[/code]
×

Success!

Help @Nightmaster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...