How do I loop through an indexed PHP array?

This should be pretty simple but all the searches I do turn up looping through an associative array. I’ve created an array in a Session contining links clicked so that when someone returns from a thank you page the links they clicked on can be disabled. When you go from the page with links to the thank you page I add it to the Session array:

//add $linkVisted array to the session
$_SESSION[‘linkVisted’] = $linkVisted;

//add the next number to the array

This works and returns the following output when I return to the links page:

Array ( [linkVisted] => Array ( [0] => 3 [1] => 3 [2] => 1 [3] => 1 [4] => 8 [5] => 9 ) )

Now, how do I loop through this array to compare each link’s number with these numbers? When I try this I get an array length of 1 - because there is only one array - so how do get the number of elements (9 here) and loop through them?

for ($i = 1; $i < count($linkVisted); ++$i) {
echo $linkVisted[$i];


How did you get this?

Array ( [linkVisted] => Array ( [0] => 3 [1] => 3 [2] => 1 [3] => 1 [4] => 8 [5] => 9 ) )

Was it the output of



foreach($linkVisted as $thisLink) {
   echo $thisLink;

if(in_array($currentLink,$linksVisited)) {
  /* current link has been previously visited */

It’s hard to see exactly what you did, but this may work:

$total = count($linkVisted['linkVisted']);
for ($i = 0; $i < $total; $i++) {
    echo $linkVisted[$i];

It’s returning 1 item because you have an array inside of an array. The first (and only item) of the outer array is the inner array. I’m not sure what you’re doing but you can either access it directly (see above), or loop through if it you have multiple inner arrays:

foreach ($linkVisted as $iLink) {
    $total = count($iLink);
    for ($i = 0; $i < $total; $i++) {
        echo $iLink[$i];

Oh, and always put computations outside the for loop (in this case, count()) because if you place it in the condition then it has to count the array for every loop, where since you know the value is going to stay constant over the scope of the loop it is faster if you place it on the outside:

for ($i = 0; $i < count($array); $i++) {
	// BAD
$total = count($array);
for ($i = 0; $i < $total; $i++) {
	// GOOD

your print out is saying that there are two arrays. Below i illustrated a litted
function to loop through if there are more than one array, otherwise print the

//assuming these are the values in your session

$_SESSION['linkVisited']= Array ( "linkVisted" => Array ( 0 => 3 ,1 => 3 ,2 => 1 ,3 => 1 ,4 => 8 ,5 => 9 ) );

	function link_visited($sess){
		foreach($sess as $kk=>$vv){
			if(is_array($vv)){ link_visited($vv); }else{ echo $vv."<br>";}

One last thing just place a global variable in your foreach loop where each result is echoed out to count the number of results. Good luck.

Thanks for the code hints. Let me explain what I’m trying to accomplish. I have a page with a set of links (seminars attended) - when you click on the links you go to a ‘thank you’ page with a ratings form. When you click ‘submit’ it takes you back to the first page of links. I want to disable the links that have already been chosen during the PHP Session.

So I have given each link a ‘link’ number that is sent through the URL to the ‘thank you’ page. I pick it up here

$linkVisted = $_GET['link'];

and then add it to a Session array

$_SESSION['linkVisted'] = $linkVisted;

Now I’m in trouble - how do I push the new link onto the array each time? Everything that I’ve tried as the first argument in ‘array_push’ returns the error ‘First argument should be an array’. What is the actual name of the Session array? $_SESSION[‘linkVisted’] doesn’t work, neither does $linkVisted.

When I return to the links page I want to write a foreach loop to check which numbers are in the array. Then I can check each link with a function call to see if the link number is in it. If it is I can disable the link.


$_SESSION['linkVisted'][] = $_GET['link'];

Thanks crmalibu - I will check this out tomorrow - just leaving work. To answer your original question - ‘how did you get this?’ ‘Was it the output of print?’ yes it was (when I got it to work) I was showing that that’s what I wanted in the array.