Is it possible to read array variables one by one in a numeric array in PHP? I have an array that I need to read each element and depending on the element, do something else. But I am finding that there are no functions to read each element of a numeric array. Help is appreciated.

should this not work

for ($i = 0; i<count($array1); $i++)
{
$j=0;
$var1 = $array1[$j];
$j++;
echo $store3;
}

My program hangs due to this loop.

Without having a chance to run the code, here are my impressions.

The line $j=0; should be above the for loop - as it is it will never move past the first element of the array because it is reset to zero at the start of each iteration of the for loop. In any case, I don't think you need it at all.

Also, what is $store3 ?

Then, i<count($array1) should be [B]$[/B]i<count($array1) . That's probably what is crashing your script.


If all you want to do is loop through the array, you don't need $j, the $i counter set in the for() statement is intended for that purpose.

Try something like this, it's much simpler and it should work;

for ($i = 0; $i<count($array1); $i++) {
  if ("1" == "1") {
    echo $array1[$i];
  }
}

Of course the if ("1" == "1") part is where you get to test for those "depending on the element, do something else" conditions.

Hope that helps.

Rob

Member Avatar for diafol
foreach($array1 as $value){
  echo $value . "<br />";
  //or if you want: if($value == 6){echo "Bad number<br />";}else{echo $value . "<br />";}
}

Without having a chance to run the code, here are my impressions.

The line $j=0; should be above the for loop - as it is it will never move past the first element of the array because it is reset to zero at the start of each iteration of the for loop. In any case, I don't think you need it at all.

Also, what is $store3 ?

Then, i<count($array1) should be [B]$[/B]i<count($array1) . That's probably what is crashing your script.


If all you want to do is loop through the array, you don't need $j, the $i counter set in the for() statement is intended for that purpose.

Try something like this, it's much simpler and it should work;

for ($i = 0; $i<count($array1); $i++) {
  if ("1" == "1") {
    echo $array1[$i];
  }
}

Of course the if ("1" == "1") part is where you get to test for those "depending on the element, do something else" conditions.

Hope that helps.

Rob

Thank you very much. u solved the problem. Actually it was the i instead of $i. I did not notice that and then I put $j. Thanks again.

Member Avatar for diafol

sorry we posted at the same time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.