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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:05:31+00:00 2026-06-05T07:05:31+00:00

I need to get an item from the json output however the json contents

  • 0

I need to get an item from the json output however the json contents aren’t always consistent.

For example if I wanted get the value for “name” it would be ['result']['attributes'][0]['name'];

But in the event that the json is delivered as the second example it would be ['result']['attributes'][1]['name'];

{"result":{
    "attributes":[
        {"user":"newb","name":"mike"},
        {"state":"california","city":"los angeles"}
    ]
}}

{"result":{
    "attributes":[
        {"state":"california","city":"los angeles"},
        {"user":"newb","name":"mike"}
    ]
}}

How would I get the “name” value if the index of the array it’s in is unknown?

  • 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-05T07:05:32+00:00Added an answer on June 5, 2026 at 7:05 am

    This will return the index into the attributes array that contains the object with a name attribute of "mike".

    function extractNameFromJson($json) {
        foreach ($json->result->attributes as $i => $attribute) {
            if (isset($attribute['name']) && $attribute['name'] == 'mike') {
                return $i;
            }
        }
    }
    
    $index = extractNameFromJson($json);
    echo $json->result->attributes[$index]['user'];
    
    ==> newb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help to get a response when I click on an Item from
I need to get the selected item name in QListView as a QString .
I have a JSON object which is generated from my website, however I need
I need to get a selected item from drop menu, I am using this
Basically, I need to get JSON data from Git's web API. Great! Did that.
I have linq request. I need get item.Title in select. how do this? var
Mysql: i need to get the offset of a item in a query. I
I have two tables with a weak relation. I need get a text value
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
I have this json, and when i get this json i need to run

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.