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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:02+00:00 2026-06-17T05:55:02+00:00

Suppose you need to create a ‘top’ of some sort and have code like

  • 0

Suppose you need to create a ‘top’ of some sort and have code like this:

$matches=array();
foreach ($array as $v){
   $matches[processing($v)]++;  
}

This will output a Notice: Undefined index for the cases the index needs creating.

What would be the best way to tackle these cases since you KNOW you’ll have to create indexes?

I used these solutions depending on case:

  1. Suppressing the error @$matches[$v]++;
    Pro: VERY easy to type
    Con: slow
  2. Checking if it’s set $matches[$v]=isset($matches[$v])?$matches[$v]++:1;
    Pro: faster
    Con: takes longer to write even in the shorthand form and need to use $matches[$v] 2 more times

Are there any other ways?
Looking for the fastest execution time as I’m using this function thousands of times or some lazier way to type that’s still faster than @

EDIT:

In a simple case where you have $matches[$v]++; you could also use array_count_values() (as Yoshi suggested)

  • 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-06-17T05:55:03+00:00Added an answer on June 17, 2026 at 5:55 am

    After some reading, writing and testing I got something:

    function inc(&$var){
        if (isset($var)) $var++;else $var=1;
    }
    

    and thought I struck gold, but let’s see the tests first…

    Test code:

    $a=array();
    
    // Pre-Fill array code goes here
    for($i=1;$i<100000;$i++) {
        $r=rand(1,30000);
        //increment code goes here
    }
    
    // Remove extra keys from array with:
    //foreach ($a as $k=>$v) if ($v==0) unset($a[$k]);
    

    Execution times: (for informative purposes only)

    inc($a[$r])                             1.15-1.24
    @$a[$r]++                                   1.03-1.09
    $a[$r]=array_key_exists($r,$a)?$a[$r]++:1;  0.99-1.04
    
    $a[$r]=!empty($a[$r])?$a[$r]++:1;               0.61-0.74
    if (!empty($a[$r])) $a[$r]++;else $a[$r]=1; 0.59-0.67
    $a[$r]=isset($a[$r])?$a[$r]++:1;                0.57-0.65
    if (isset($a[$r])) $a[$r]++;else $a[$r]=1;  0.56-0.64
    
    
    //with pre-fill
    $a=array_fill(0,30000,0);                   +0.07(avg)
    for($i=1;$i<=30000;$a[$i++]=0);             -0.04(avg)
    
    //with pre-fill and unset
    $a=array_fill(0,45000,0);                   +0.16(avg)
    for($i=1;$i<=45000;$a[$i++]=0);             +0.02(avg)
    

    Conclusions:

    • @ is of course the fastest to type and I don’t see any problem in using it in this case but feel free to check this question also: Suppress error with @ operator in PHP
    • completely suppressing errors (before the loop and enabling errors after the loop) via ini_set() is worse than all on performance
    • inc() looks nice and clean, easy to type and does checking instead of suppressing, but calling it looks to be even slower than @
    • isset() is slightly faster than empty(), but both perform fairly the same
    • interestingly using shorthand if statements is slightly slower!
    • best results achieved when pre-filling the array. Even if length is unknown a good prediction would be still slightly faster on a huge dataset
    • strangely, array_fill() takes slightly longer than for ?!?!

    RFC

    I don’t consider this answer 100% complete, although, for now it looks like isset() is the fastest and @ the laziest.
    Any comments and ideas are appreciated!

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

Sidebar

Related Questions

Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose I have an array of nodes (objects). I need to create a duplicate
Suppose I have the need to do the following (This is just some imaginative
Suppose I create an element using $(' some html ') , like this: var
Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes (
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
Can't figure out how to create an array with all matches. I suppose I
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I have some issues in Rails Active Record Inheritance, I need to create a
Let's suppose I need to perform some average-length (several days) refactoring. I create mybranch

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.