1

I was creating an array and I need to repeat this array base on the number loop. How can I repeat in a for loop.

 for ($i = 0; $i <= 10; $i++) {
     $array = array(
       'name'          => 'Name 1',    
       'id'            => '1',
       'items'         => array(
          array('name'  => 'item 1', 'id'  => 'itemid1'),
          array('name'  => 'item 2', 'id'  => 'itemid2'),
     )
  );
 }
2
  • So you are asking for dynamic number? Commented Aug 3, 2019 at 14:14
  • No. my intention is to loop that 1 array into number of specified number sample 10. It will generate 10 arrays using that one array. Commented Aug 3, 2019 at 14:44

1 Answer 1

1

Append new array instead rewriting:

$array = [];

for ($i = 0; $i <= 10; $i++) {
     // Here
     $array[] = [
       'name'          => 'Name 1',    
       'id'            => '1',
       'items'         => [
          ['name'  => 'item 1', 'id'  => 'itemid1'],
          ['name'  => 'item 2', 'id'  => 'itemid2'],
     ],
  ];
}
Sign up to request clarification or add additional context in comments.

Comments

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.