0

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.

Edited by raghujosh: n/a

3
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by diafol
0

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

Edited by scaiferw: n/a

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

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.