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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:42:04+00:00 2026-05-21T09:42:04+00:00

I’m really stuck trying to resolve what should be quite simple. I Have this

  • 0

I’m really stuck trying to resolve what should be quite simple.

I Have this

<?php 

$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

 $id = array('33540116', '33541502');


foreach($id as $id) {

        echo $json->$id->person->{'name'}. '<br />';
        echo $json->$id->person->{'age'}. '<br />';


        }
?>

So the code is decoding a json string then using foreach to echo each result.

This json file is rather large and I’m only interested in certain records that match the id’s stored in a mysql table.

To do this I have replaced the id array string above with mysql select statement.

<?php
$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

$result = mysql_query("SELECT id FROM people");
$row = mysql_fetch_array($result);

$id = array($row['id']);

foreach($id as $id) {

            echo $json->$id->person->{'name'}. '<br />';
            echo $json->$id->person->{'age'}. '<br />';


            }
?>

Although this works, it only gives me 1 result.
What I really need is to loop through the results.
Unfortunately I don’t know how to construct a while loop together with foreach.

I will greatly appreciate your assistance.

UPDATE (extra question)

Thanks everyone. You have helped me solve the problem.

However, I have another question that relates to this matter.

I mentioned above that I merely wanted to echo the results.
But this isn’t exactly true.
What I really want to do is update the same mysql table with the results retreived from the json file.

I have a table called people with fields id, name and age.

How can I update this table with these results?

Thanks again.

  • 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-21T09:42:04+00:00Added an answer on May 21, 2026 at 9:42 am

    mysql_fetch_array only fetches one row at a time. You can use a while loop to continue fetching rows. The mysql_fetch_array function returns false once the whole result set has been fetched, so that will cause the while loop to terminate as desired.

    Also, I removed the foreach loop on $ids. Since there will only be one element in the array it’s unnecessary to put the code in a loop.

    <?php
    $json = json_decode('{
                          "33540116":
                                     {"person":
                                               {"name":"John", "age":"36"}},
                          "33541502":
                                     {"person":                                                                                      
                                               {"name":"Jack", "age":"23"}}
                         }
            ');
    
    $result = mysql_query("SELECT id FROM people");
    
    while ( ( $row = mysql_fetch_array($result) ) ) {
        $id = $row['id'];
    
        echo $json->$id->person->{'name'}. '<br />';
        echo $json->$id->person->{'age'}. '<br />';
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.