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 6982481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:17:40+00:00 2026-05-27T18:17:40+00:00

For readability and perfomance reasons, I’d like to build up an array with a

  • 0

For readability and perfomance reasons, I’d like to build up an array with a switch statement instead of if-statements.

Consider the following if-statement:

$size = 2;
$array = array();
if($size >= 1) { array_push($array,'one','foo'); }
if($size >= 2) { array_push($array,'two','bar','barista'); }
if($size >= 3) { array_push($array,'three','zoo','fool','cool','moo'); }

It basically counts up from 1 to $size, it might be more readable and most likely a lot faster with a switch-statment…but how do you construct that ??

$step = 2;
$array = array();
switch($step)
{
    case ($step>1): array_push($array,'one','foo');
    case ($step>2): array_push($array,'two','bar','barista');
    case ($step>3): array_push($array,'three','zoo','fool','cool','moo');
}

I tried leaving out the break, which didn’t work – as the manual says:

In a switch statement, the condition is evaluated only once[…].
PHP continues to execute the statements until the end of the switch
block, or the first time it sees a break statement.

Anyway, anyone got an idea how to build up such an array with a switch-statement ??

  • 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-27T18:17:41+00:00Added an answer on May 27, 2026 at 6:17 pm

    Surely what you want can be achieved much more easily using

    $array=range(1,$size);
    

    Based on further coments and subsequent edits, something like:

    $baseArray = $array(array('one'),
                        array('two','twoA'),
                        array('three','threeA','threeB'),
                        array(),
                        array('five'),
                       );
    $step=2;
    $array = array_slice($baseArray,0,$step);
    

    and then flatten $array

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's better way to format following sql statement considering both readability and performance. Thanks.
Besides readability is there any significant benifit to using a CASE WHEN statement vs
Which one is recommended considering readability, memory usage, other reasons? 1. String strSomething1 =
with: import numpy as np array = get_array() I need to do the following
So I've been reading a lot today about the with statement performance, readability and
Please ignore code readability in this question. In terms of performance, should the following
what is the best solution in terms of performance and readability/good coding style to
Is there a difference in performance (I am not asking about readability) if I
is it better to do this (regarding performance, not readability...): $('a.updateCartButton').click(function() { $('form[name=updateCartForm]').attr('action', $(this).attr('href')
I always thought that parentheses improved readability, but in my textbook there is a

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.