Jump to content

help with array foreach


newbtophp

Recommended Posts

I have 2 arrays with the the same amount of elements, i want to loop through them both and then link the titles with the $pages (href location). Currently its not echoing what I expect it too (presumably due to me looping it incorrectly?)

 

Currently its outputting :(:

 

<a href="life-death-peter-sellers">Peter Pan</a><br>
<a href="life-death-peter-sellers">Peter and Vandy</a><br>
<a href="life-death-peter-sellers">The Life and Death of Peter Sellers</a><br>
<a href="peter-and-vandy">Peter Pan</a><br>
<a href="peter-and-vandy">Peter and Vandy</a><br>
<a href="peter-and-vandy">The Life and Death of Peter Sellers</a><br>
<a href="peter-pan">Peter Pan</a><br>
<a href="peter-pan">Peter and Vandy</a><br><a href="peter-pan">The Life and Death of Peter Sellers</a><br>

$pages contains:

 

Array ( [0] => life-death-peter-sellers [1] => peter-and-vandy [2] => peter-pan ) 

 

$titles contains:

 

Array ( [0] => Peter Pan [1] => Peter and Vandy [2] => The Life and Death of Peter Sellers ) 

 

My current code:

 

foreach($pages as $page){
foreach($titles as $title){
echo "<a href=\"".$page."\">".$title."</a><br>";
}
}

 

Expected Output:

 

<a href="life-death-peter-sellers">Peter Pan</a><br>
<a href="peter-and-vandy">Peter and Vandy</a><br>
<a href="peter-pan">The Life and Death of Peter Sellers</a><br>

 

All help is appreciated  :-\ :-\

Link to comment
Share on other sites

because you have nested for loops you $page array is looping through every $title value, hence getting 3x the output.

 

if you only have a 1-1 relationship (one page per title and vice versa) on your values just do:

for($i =0; $i < count($page); $i++){

echo "<a href=\"".$page[$i]."\">".$title[$i]."</a><br>";

}

 

other option would be to combine those two arrays.

 

Link to comment
Share on other sites

Thanks figured that aspect out, but got another issue,

 

Say if I have an array like:

 

Array ( [0] => peter-pan [2] => life-death-peter-sellers [4] => peter-and-vandy )

 

How can I turn it into:

 

Array ( [0] => peter-pan [1] => life-death-peter-sellers [2] => peter-and-vandy )

 

I've tried sort but that rearrange the grouping, I want the elements to stay in the same place, but ensure that their in numerical order.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.