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
add comment

3 Answers

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
add comment
for ($i = 1; $i <= 6; $i++)
    $ar[] = $i;
share|improve this answer
add comment
 for ($i=1;$i<7;$i++)
 {
      $ar[] = $i;
 }
share|improve this answer
 
This would come up with wrong result. –  OhhMee 22 hours ago
add comment

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.