Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to make an array like this

$array = array("'firstName1', 'lastName1'", "'firstName2', 'lastName2'", ......);

I always get an error like this:

Parse error: syntax error, unexpected 'while' (T_WHILE), expecting ')' in C:\wamp\www\Tests\index.php on line 11

<!doctype html>
<html>
<head>
    <meta charset="utf=8">
</head>
<body>
<?php
if(isset($_POST['reg'])){
    $x=1;
    $a = array(
    while($x<=10):
    "'firstName$x', 'lastName$x'"; //I DONT KNOW WHAT TO DO IN THIS LINE//
    $x++;
    endwhile;
    );
    print_r($a);
}
?>
<form action="" method="post">
<input type="text" name="number" /> <input type="submit" name="submit" value="Submit"/>
</form>
    <form action="" method="post">
    <table>
    <?php
    if(isset($_POST['submit'])){
    for($i=1;$i<=$_POST['number'];$i++){
    echo "<tr>
    <td><input type='text' name='firstName$i' /></td>
    <td><input type='text' name='lastName$i' /></td>
    </tr>";
    }
    $i-=1;
    echo "<input type='hidden' name='hide' value='$i' />";
    }           
    ?>
    </table>
    <input type="submit" value="Register" name="reg"/>
    </form>
</body>
</html>
share|improve this question
thank you for checking my mistakes! =) – Kevin Steve Maningo Jul 26 '12 at 6:59

3 Answers

up vote 7 down vote accepted
$a = array();

while($x<=10):
    $a[] = 'firstName'.$x;
    $a[] = 'lastName'.$x;
    $x++;
endwhile;

I read your question again, if you want this "'firstName1', 'lastName1'" to be actually a string then,

$a = array();
while($x<=10):
    $a[] = 'firstName'.$x.'lastName'.$x;
    $x++;
endwhile; 

Or based on your question title (nested array) then,

$x = 1;
while($x<=10):
   $a[] = array('firstName'.$x, 'lastName'.$x);
   $x++;
endwhile;
share|improve this answer
No need to declare the array first. You're doing that with the short syntax. – RobB Jul 26 '12 at 6:07
thank you so much.. this is exactly what I need.. but what if I want to make it like this $a[] = "'$_POST[firstName$x]', '$_POST[lastName$x]'"; <br/> and the output would be like this ('Kevin Steve', 'Maningo') – Kevin Steve Maningo Jul 26 '12 at 6:21
use this $a[] = $_POST["firstName".$x].", ".$_POST["lastName".$x]; – Leon Jul 26 '12 at 6:33
Thank you, its done. I made it like this $a[] = "'".$_POST["firstName".$x].", ".$_POST["lastName".$x]."'"; and it works as I want it too.. thanks – Kevin Steve Maningo Jul 26 '12 at 6:42

May this help you.

$x = 1;
while($x <=10 ):    
    $a[] = '"\'firstName'.$x.'\', \'lastName'.$x.'\'"';    
    $x++;
endwhile;
echo '<pre>';
print_r($a);
echo '</pre>';

Output is :

Array
(
    [0] => "'firstName1', 'lastName1'"
    [1] => "'firstName2', 'lastName2'"
    [2] => "'firstName3', 'lastName3'"
    [3] => "'firstName4', 'lastName4'"
    [4] => "'firstName5', 'lastName5'"
    [5] => "'firstName6', 'lastName6'"
    [6] => "'firstName7', 'lastName7'"
    [7] => "'firstName8', 'lastName8'"
    [8] => "'firstName9', 'lastName9'"
    [9] => "'firstName10', 'lastName10'"
)
share|improve this answer
if(isset($_POST['reg'])){
$x=1;
$a = array();
while($x<=10){
 $a[] = "firstName$x";
 $a[] = "lastName$x";
 $x++;
}

print_r($a);

}

share|improve this answer
I'm not sure using double quotes quite qualifies for a duplicate answer since the variable expansion is not improving anything over concatenation. – RobB Jul 26 '12 at 6:09
@RobB, well, I got some times error when I used the variable in single quotes as used by the asker :) , I think it is best practice to use the variables in double quotes rather than single quotes. – Suleman Jul 26 '12 at 6:16

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.