0

I have the following code:

for ($i = 1; $i <= $j; $i++) 
{
    $goods_{$i} = array(
        $_POST["'goods'.$i'_title'"],
        $_POST["'goods'.$i.'_package'"],
        $_POST["'goods'.$i.'_nmr'"]
    );
}

I hoped that it could make this in first step of the cycle:

$i =1;
$goods_1 = array(
    $_POST['goods1_title'], 
    $_POST['goods1_package'], 
    $_POST['goods1_nmr']
);

and so on in other steps.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Just get rid of the single quotes and dots in the double-quoted strings: `$_POST["goods{$i}_title"]` – jeroen Apr 28 '14 at 18:25
  • @jeroen This didn't help. Somewhere is syntax mistake, because the white screen still appears. –  Apr 28 '14 at 18:34
  • Then [enable error reporting](http://stackoverflow.com/a/6575502/1438393) and find out where the syntax error occurs. – Amal Murali Apr 28 '14 at 18:35
  • 4
    Why in the world are you doing this? You are using arrays, keep using them. – AbraCadaver Apr 28 '14 at 18:38

3 Answers3

1

It can be done like this:

for ($i = 1; $i <= $j; $i++) 
{
    ${"goods_$i"} = array(
        $_POST["'goods'.$i'_title'"],
        $_POST["'goods'.$i.'_package'"],
        $_POST["'goods'.$i.'_nmr'"]
    );
}

You can read more about this topic in related PHP documentation.

The result of "'goods'.$i'_title'" will be 'goods'.1'_title', in case that you want it to be goods1_title then use following code instead:

for ($i = 1; $i <= $j; $i++) 
{
    ${"goods_$i"} = array(
        $_POST["goods{$i}_title"],
        $_POST["goods{$i}_package"],
        $_POST["goods{$i}_nmr"]
    );
}

Another bug might be that in 1 case you use .$i. and in other 2 cases you use .$i without the last ..

1

Should be

    $_POST["goods{$i}_title"],
    $_POST["goods{$i}_package"],
    $_POST["goods{$i}_nmr"]
nicael
  • 18,550
  • 13
  • 57
  • 90
  • 2
    @DanyCaissy Why are you saying lies? I can't down vote. It requires 125 reputation. –  Apr 28 '14 at 18:41
  • Problem is that creating variable of variable does not work this way `$goods_{$i}`. That is probably why your answer was downvoted. –  Apr 28 '14 at 18:49
  • Try to build the variable that way and after that use `var_dump($goods_1)`, you will get `NULL`. –  Apr 28 '14 at 18:50
  • please add some explanation of your code, and explain what the OP was doing wrong – Our Man in Bananas Apr 28 '14 at 19:02
1

I follow AbraCadaver's sentiments:

Why in the world are you doing this? You are using arrays, keep using them.

As such, I would write the code simply using an Array:

$goods = array();
for ($i = 1; $i <= $j; $i++) 
{
    // Assign to an index in the already created array,
    // but DO NOT create a new variable.
    $goods[$i] = array(
        // Also make sure these are correct ..
        $_POST["goods{$i}_title"],
    );
}

If you really want to create dynamic variables - ick! - see variable variables.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • @nicael Your answer discussed the secondary string building which I address in passing. I believe the primary issue is the `$goods_$i` syntax/goal. – user2864740 Apr 28 '14 at 18:58
  • "As such, I would write the code simply as:" - you can write it even more simple ... :) Just define array as `$goods = []`. –  Apr 28 '14 at 19:02
  • @JakubPolák Old/familiar habits die hard - that would work, yes. – user2864740 Apr 28 '14 at 19:04