Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to create array from variable php using loop for ?

i have many variable php like

$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;

i want to create array like this

$ar = array('1','2','5','2','6');

but using loop for like

for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
share|improve this question

3 Answers 3

up vote 7 down vote accepted

Not a recomended way of doing things but:

$arr = array();
for($i=0;$i<5;$i++) {
    $varName = 'number_'.$i;
    $arr[] = $$varName;
}
share|improve this answer
for ($i = 1; $i <= 6; $i++)
    $ar[] = $i;
share|improve this answer
 for ($i=1;$i<7;$i++)
 {
      $ar[] = $i;
 }
share|improve this answer
    
This would come up with wrong result. –  OhhMee Jan 6 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.