使用foreach [关闭]向数组添加键和值

$arr = array(

      foreach($data as $i => $c):
            $sub[$i] = $c;
      endforeach;

);

What is wrong with the construction of this loop?

You want:

  $sub = array();
  foreach ($data as $i => $c):
        $sub[$i] = $c;
  endforeach;

or

  $sub = array();
  foreach ($data as $i => $c) {
        $sub[$i] = $c;
  }

Your code is invalid beacause you cannot use statements (like foreach) as array argument.

What's more, your code just copies one array into another, I don't want what's the purpose. I think you should read some good PHP manual.

I would say, simply becuase it's wrong.

You can't execute code within the array() arguments.

What you want to do is to inject the data, in your empty array, like this:

  $sub = array();
  foreach($data as $i => $c)
        $sub[$i] = $c;