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

  • Home
  • SEARCH
  • 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 6926721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:57:19+00:00 2026-05-27T10:57:19+00:00

As we all know, json_decode gives you the option of returning an associative array

  • 0

As we all know, json_decode gives you the option of returning an associative array or an object. There are many other situations where we have the two options as well. Obviously in some cases using one or the other is more “appropriate” based on the type of data you’re dealing with (a group of data pertaining to one item vs. a list of items).

What I’m wondering is, is there any difference in efficiency in using one vs. the other, in terms of memory, speed, etc? I’m particularly interested in access time for a very large object – how does that compare to a very large array, and why?

Sorry if this has been discussed before, search turned up nothing. I’m writing a benchmark, which may tell me which one is better, but won’t help me understand why.

  • 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-27T10:57:20+00:00Added an answer on May 27, 2026 at 10:57 am

    Many programmers prefer using true as the second argument to json_decode since the returned assoc array will be very similar to how you handle objects in javascript.

    Returning a proper object will require reading about how such is used and what not, and since most programmers are well familiar with associative arrays that’s more preferrable, especially if the code will be maintained by a team of developers. Code should be easily understandable.

    Regarding questions about performance I don’t think you’ll need to worry about that since the bottle neck in most (all) cases will be elsewhere. Unless you are parsing a massive string, and by that I mean really huge, you shouldn’t need to make any benchmarks. I believe the difference between returning an assoc array vs a proper object will be minor.


    Performance Benchmark (parsing)

    I found a rather large json string here and made some adjustments to make it even bigger, final size is 84 578 bytes.

    I then parsed the string using both alternatives (associative array vs object) 1 000 times each, and I ran the test three times. The results are given below:

    1st run

      JSON object exec: 4.06122 s
      JSON assoc  exec: 3.28679 s
    -------------------------------------
    assoc is faster by 19.07%
    

    2nd run

      JSON object exec: 4.09614 s
      JSON assoc  exec: 3.29216 s
    -------------------------------------
    assoc is faster by 19.63%
    

    3rd run

      JSON object exec: 4.08762 s
      JSON assoc  exec: 3.29960 s
    -------------------------------------
    assoc is faster by 19.28%
    

    Performance Benchmark (read/write)

    This benchmark is to show which one of stdObject and Array() is faster, I’m using a parsed modified json file (bigger one) than in the previous benchmark.

    Each read/write test was run 100 000 times (ie. the code given below was executed that many times).

    json_decode ($json_data)

    for ($i =0; $i < 24; ++$i){
      $a = $object[$i]->user->profile_sidebar_border_color . "stackoverflow";
      $object[$i]->nested->entities->user_mentions[0]->indices[$i&1] += 1;
    }
    

    json_decode ($json_data, true)

    for ($i =0; $i < 24; ++$i){
      $a = $assoc[$i]['user']['profile_sidebar_border_color'] . "stackoverflow";
      $assoc[$i]['nested']['entities']['user_mentions'][0]['indices'][$i&1] += 1;
    }
    

    1st run

      JSON object read/write: 3.05421 s
      JSON assoc  read/write: 2.51932 s
    -------------------------------------
    assoc is faster by 17.51%
    

    2nd run

      JSON object read/write: 3.06307 s
      JSON assoc  read/write: 2.52701 s
    -------------------------------------
    assoc is faster by 17.50%
    

    3rd run

      JSON object read/write: 3.06109 s
      JSON assoc  read/write: 2.52248 s
    -------------------------------------
    assoc is faster by 17.60%
    

    PHP version

    PHP 5.3.6 (cli) (built: Aug 13 2011 19:04:57) Copyright (c) 1997-2011

    The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend

    Technologies

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

Sidebar

Related Questions

I have a JSON array that I know is valid. The array shows all
We all know you can't do the following because of ConcurrentModificationException : for (Object
I find myself converting between array and object all the time in PHP application
I know there have been similar questions posted but I think the issue I'm
Does anyone know why this happens? var_dump(json_decode(stripslashes(json_encode(array(O'Reiley))))); // array(1) { [0]=> string(8) O'Reiley }
$class = new Class; $foo = json_decode($_POST['array']); In this highly contrived example, I have
I've got some JSON data, but it's all on one line. Does anyone know
We all know T-SQL's string manipulation capabilities sometimes leaves much to be desired... I
We all know how to use <ctrl>-R to reverse search through history, but did
We all know that RAW pointers need to be wrapped in some form of

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.