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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:44:07+00:00 2026-05-14T02:44:07+00:00

I know how to decode a JSON string with one object with your help

  • 0

I know how to decode a JSON string with one object with your help from this example How to decode a JSON String

But now I would like to improve decoding JSON string with several objects and I can’t understand how to do it.

Here is an example:

{ "inbox": [
  { "firstName": "Brett", "lastName":"McLaughlin" },
  { "firstName": "Jason", "lastName":"Hunter" },
  { "firstName": "Elliotte", "lastName":"Harold" }
 ],
"sent": [
  { "firstName": "Isaac", "lastName": "Asimov" },
  { "firstName": "Tad", "lastName": "Williams" },
  { "firstName": "Frank", "lastName": "Peretti" }
 ],
"draft": [
  { "firstName": "Eric", "lastName": "Clapton" },
  { "firstName": "Sergei", "lastName": "Rachmaninoff" }
 ]
}
  1. How to make just one foreach() to
    decode above JSON string?
  2. How to detect object’s names: inbox,
    sent or draft on this foreach()?
  • 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-14T02:44:08+00:00Added an answer on May 14, 2026 at 2:44 am

    New answer

    Re your revised question: foreach actually works with properties as well as with many-valued items (arrays), details here. So for instance, with the JSON string in your question:

    $data = json_decode($json);
    foreach ($data as $name => $value) {
        // This will loop three times:
        //     $name = inbox
        //     $name = sent
        //     $name = draft
        // ...with $value as the value of that property
    }
    

    Within your main loop over the properties, you can use an inner loop to go over the array entries each property points to. So for instance, if you know that each of the top-level properties has an array value, and that each array entry has a “firstName” property, this code:

    $data = json_decode($json);
    foreach ($data as $name => $value) {
        echo $name . ':'
        foreach ($value as $entry) {
            echo '  ' . $entry->firstName;
        }
    }
    

    …will show:

    inbox:
      Brett
      Jason
      Elliotte
    sent:
      Issac
      Tad
      Frank
    draft:
      Eric
      Sergei

    Old answer(s)

    Begin edit
    Re your comment:

    Now I would like to know how to decode JSON string with several objects!

    The example you posted does have several objects, they’re just all contained within one wrapper object. This is a requirement of JSON; you cannot (for example) do this:

    {"name": "I'm the first object"},
    {"name": "I'm the second object"}
    

    That JSON is not valid. There has to be a single top-level object. It might just contain an array:

    {"objects": [
        {"name": "I'm the first object"},
        {"name": "I'm the second object"}
    ]}
    

    …or of course you can give the individual objects names:

    {
        "obj0": {"name": "I'm the first object"},
        "obj1": {"name": "I'm the second object"}
    }
    

    End edit

    Your example is one object containing three properties, the value of each of which is an array of objects. In fact, it’s not much different from the example in the question you linked (which also has an object with properties that have array values).

    So:

    $data = json_decode($json);
    foreach ($data->programmers as $programmer) {
        // ...use $programmer for something...
    }
    foreach ($data->authors as $author) {
        // ...use $author for something...
    }
    foreach ($data->musicians as $musician) {
        // ...use $musician for something...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got this result using json decode from an api call. but I
I receive reply from Google Translate v2 as json which looks like this {
i have this JSON string that i want to decode it with json_decode(); function
I have a gzipped base64 JSON string, after converting this string json_decode() can't decode
I need to decode a JSON string with the float number like: {name:Galaxy Nexus,
I was having a question about making a 2D JSON string Now I would
I would really like to know how web sites decide what ad to show
Know this might be rather basic, but I been trying to figure out how
for this json api response I am able to parse with json_decode: $string =
would any of you know a good way to put this into an associative

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.