Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4088206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:57:09+00:00 2026-05-20T18:57:09+00:00

I just wrote this up, is this the most efficient way to add arrays

  • 0

I just wrote this up, is this the most efficient way to add arrays to a preexisting array.


$c=4;
$i=1;
$myarray = array();

while($i <= $c):
    array_push($myarray, array('key' => 'value'));
    $i++;
endwhile;

echo '<pre><code>';
var_dump($myarray);
echo '</code></pre>';

Update: How would you push the key & value, without creating a new array.
so this array_push($myarray,'key' => 'value');
not this array_push($myarray, array('key' => 'value'));

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:57:09+00:00Added an answer on May 20, 2026 at 6:57 pm

    Your code has a couple of things which can be improved:

    Magic Numbers

    It’s a bad practice to assign magic numbers like 4 and 1, use constants instead. For this example it’s of course overkill but still important to know and use.

    Lack of braces

    Always use the curly braces, it makes the code more readable.

    Wrong use of while loop

    This is not a case for a while loop, if you want to loop a certain number of times, always use a for loop!

    Unnessesary use of array_push

    You don’t need array push to add elements to an array, you can and should probably use the shorthand function.

    Result:

    define('START', 1);
    define('END', 4);
    
    $myArray = array();
    
    
    for ($i = START; $i < END; $i++)
    {
        $myArray[] = array('item' => '1 items');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most efficient way of reading just part of the binary data
What's the most efficient way to read a stream into another stream? In this
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I'm really confused, I just wrote this x = 0 y = 42 #Can
Just for fun, I wrote this simple function to reverse a string in Python:
I wrote a program to solve a complicated problem. This program is just a
What would be the most efficient way of recording to a log (.txt) from
Would the most efficient way-and I know it's not very efficient, but I honestly
I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
I might be insane to write this here, but I am just too scared

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.