0

To limit the use of [xx] in my code I would like to do something C/C++ style :

$nbItem = 30;
items = array($nbItem);

foreach ($items as $i => $item){
    $item = new Entity();
    $item->setToto();
    // do other stuff
}

Of course it didn't work, is there a typo or is just impossible in php ?

Note

I know how to do the same with a for ($i ....) loop so my question is specifically on foreach

Krupal Panchal
  • 1,553
  • 2
  • 13
  • 26
Pierrick Rambaud
  • 1,726
  • 1
  • 20
  • 47

3 Answers3

2

You can use array_fill method. e.g in your code

$nbItem = 30;
items = array_fill(0,$nbItem,0);

foreach ($items as $i => $item){
    $item = new Entity();
    $item->setToto();
    // do other stuff
}

In your code, you are trying to push the 30 into array which is at 0 index, ultimately it has a size of 1

If you dont want to assign 0 to every index you can use range e.g

$nbItem = 30;
items = range(0,$nbItem);

foreach ($items as $i => $item){
    $item = new Entity();
    $item->setToto();
    // do other stuff
}

i would recommend this solution cz its neater

Pierrick Rambaud
  • 1,726
  • 1
  • 20
  • 47
Danial Shabbir
  • 612
  • 8
  • 18
  • I come from the documentation. Sadly the definition isn't like `array_fill($num, $value=0, $start_item=0)` So I'm force to set them all. thx – Pierrick Rambaud Nov 28 '19 at 15:12
  • I prefer the second. For future answer consider adding the links to the documentation when you present functions it's easier for noobs as me – Pierrick Rambaud Nov 28 '19 at 15:44
  • **very important comment** if you intent to use this solution and change the variable. make sur that the value is passed by reference such as : `foreach(items as &$item)`. The explainations are [here](https://stackoverflow.com/a/15024638/6734243) – Pierrick Rambaud Nov 28 '19 at 16:04
0

There is no way to declare array size as you suggested. If you MUST use foreach you can try this way:

$items = array_fill(0, $nbItem, null);

foreach($items as $i => &$item) {
    $item = new Entity();
    $item->setToto();
    // do other stuff
}

But I strongly discourage you to use this solution. Just do it in a for loop:

$items = [];

for($i = 0; $i < $nbItem; $i++) {
    $item = new Entity();
    $item->setToto();
    // do other stuff

    $items[] = $item;
}
kaczmen
  • 528
  • 4
  • 12
0

There are two things you have to pay attention regarding the code you posted.

array(30) does not produce an array with 30 items but one array having 30 as its only element.
For an array with 30 items you can use array_fill().

foreach ($items as $i => $item) -- $item is not a reference but a copy of an element of the array.
You then store something else in $item ($item = new Entity();) and the array seems to be used only to count to 30.

If your intention is to use the array only to count to 30 then the easiest way is to generate the array using range():

foreach (range(1, 30) as $i) {
  $item = new Entity();
  $item->setToto();
}
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Wouldn't a `for` loop be [sightly] better? Foreach seems to be using each value (or maybe key) when in the exampled loop the value of the counter is not significant. – Martin Nov 28 '19 at 15:59
  • the foreach loop is faster in any case. I just wanted to initialize an array of 30 empty slots. – Pierrick Rambaud Nov 28 '19 at 16:07