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

3
  • your array $items is not an empty array Commented Nov 28, 2019 at 15:01
  • You use $item for various things (the value from the foreach and the new Entity()) Commented Nov 28, 2019 at 15:06
  • Show us an example. Commented Nov 28, 2019 at 15:07

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
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
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
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;
}

2 Comments

You have no Idea how many instructions I need to set and how painful it is for my eyes to see so many [x] :)
@PierrickRambaud check updated answear with for example. The is only one [] more.
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();
}

2 Comments

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.
the foreach loop is faster in any case. I just wanted to initialize an array of 30 empty slots.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.