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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:33:07+00:00 2026-06-04T18:33:07+00:00

When using json_encode for a multidimensional array in PHP, I’m noticing an output when

  • 0

When using json_encode for a multidimensional array in PHP, I’m noticing an output when naming one of the arrays, but different output when I am not naming them. For Example:

$arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));
json_encode($arrytest)

gives a single array of multiple json objects

[{"a":1,"b":2},{"c":3},{"d":4}];

whereas simply assigning a name to the middle array

$arrytest = array(array('a'=>1, 'b'=>2),"secondarray"=>array('c'=>3),array('d'=>4));
json_encode($arrytest)

creates a single json object with multiple json objects inside

{"0":{"a":1,"b":2},"secondarray":{"c":3},"1":{"d":4}};

why would the 1st option not return the same reasults like the 2nd except with "1" in place of "secondarray"

  • 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-04T18:33:09+00:00Added an answer on June 4, 2026 at 6:33 pm

    In JSON, arrays [] only every have numeric keys, whereas objects {} have string properties. The inclusion of a array key in your second example forces the entire outer structure to be an object by necessity. The inner objects of both examples are made as objects because of the inclusion of string keys a,b,c,d.

    If you were to use the JSON_FORCE_OBJECT option on the first example, you should get back a similar structure to the second, with the outer structure an object rather than an array. Without specifying that you want it as an object, the absence of string keys in the outer array causes PHP to assume it is to be encoded as the equivalent array structure in JSON.

    $arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));
    
    // Force the outer structure into an object rather than array
    echo json_encode($arrytest , JSON_FORCE_OBJECT);
    
    // {"0":{"a":1,"b":2},"1":{"c":3},"2":{"d":4}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I convert my php array to json using json_encode, Than I record this json
I have a PHP file using json_encode to make a single call, but it
I am generating json from an array using json_encode() , it's working properly, but
When using PHP's json_encode to encode an array as a JSON string, is there
I have a php multidimensional array populated from a table using the following code:
I am using JSON_ENCODE in PHP to output data. When it gets to this
When decoding/encoding a utf8 string using json_encode/json_decode I do not get back the string
Background: Using jQuery 1.7 client side PHP server side Using json responses with json_encode
Does anyone know there have any other way that (by not using json_encode and
This piece of valid json (it has been generated using php's json_encode): {html:form is

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.