0

I'm trying to add values to an array in a while loop in php, however, I can't seem to do so. Values are added to the array if I just test with strings, but using variables doesn't work. The variables ($array[$j][0]) can be echoed by themselves but not added to the added array.

while($j>0){
    $added=array();
    $added[]=$array[$j][0];
}
print_r($added);
dfitz
  • 1
  • 2
  • 1
    `$array[$j][0]` is always empty & in every iteration you are resetting the array. – Sougata Bose Jan 21 '16 at 04:55
  • 1
    Move `$added = array();` outside of the while loop. Plus, `$j` is never incremented, and `$array[0][0]` wouldn't exist. Can you go into detail of what you're actually trying to do? – Dave Chen Jan 21 '16 at 04:57
  • Thank you! Yup was clearing the array every iteration. Thanks! – dfitz Jan 21 '16 at 05:01

2 Answers2

2

Each time $added array is getting reset(empty) in while loop. Use below code

    $added=array();
while($j>0){
    $added[]=$array[$j][0];
}
print_r($added);
1

You are resetting the value of $added to equal an empty array inside your while loop. Try moving $added=array(); outside of the loop, before the while. I think you're missing part of your code snippet - but that is one issue I'm seeing.

Spencer Rohan
  • 1,759
  • 3
  • 12
  • 23