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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:03:43+00:00 2026-05-29T19:03:43+00:00

My php side $i = 1; while ($row = mysql_fetch_array($query)) { $productarray[$i][] = $row[‘product_id’];

  • 0

My php side

   $i = 1;
   while ($row = mysql_fetch_array($query)) {
       $productarray[$i][] = $row['product_id'];
       $productarray[$i][] = $row['product_model'];
       $productarray[$i][] = $row['product_type'];
       $productarray[$i][] = $row['product_return'];
       $i++;
    }
    $jsonstring = json_encode($productarray);

This is what im getting Is this a valid json string

{
 "1":["1","HFJ5G1.5","plat","graviteits"],
 "2":["2","HHJ5S2.5","holle plunjer","veer"],
 "3":["3","HTJ5S7.5","inbouw","veer"]
}

i cant iterate through this someone please tell me how to iterate this json

From the json site i think this is the format if so how can i change the above json string to this format ?

{
 "1":[{"1","HFJ5G1.5","plat","graviteits"}],
 "2":[{"2","HHJ5S2.5","holle plunjer","veer"}],
 "3":[{"3","HTJ5S7.5","inbouw","veer"}]
}
  • 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-29T19:03:51+00:00Added an answer on May 29, 2026 at 7:03 pm

    You need to refactor your PHP code a tad bit. Try doing something like this:

    $productarray = array();
    while ($product = mysql_fetch_assoc($query)) { // NOTE: using associative array
        $productarray[] = $product;
    }
    echo json_encode($productarray);
    
    // Output
    // [{
    //     "product_id": "1",
    //     "product_model": "HFJ5G1.5",
    //     "product_type": "plat",
    //     "product_rturn": "graviteits"
    // }, {
    //     "product_id": "2",
    //     "product_model": "HHJ5S2.5",
    //     "product_type": "holle plunjer",
    //     "product_rturn": "veer"
    // }, {
    //     "product_id": "3",
    //     "product_model": "HTJ5S7.5",
    //     "product_type": "inbouw",
    //     "product_rturn": "veer"
    // }]
    

    Notice that it now becomes very easy to iterate. You can do so as follows:

    var o = [{
        "product_id": "1",
        "product_model": "HFJ5G1.5",
        "product_type": "plat",
        "product_rturn": "graviteits"
    }, {
        "product_id": "2",
        "product_model": "HHJ5S2.5",
        "product_type": "holle plunjer",
        "product_rturn": "veer"
    }, {
        "product_id": "3",
        "product_model": "HTJ5S7.5",
        "product_type": "inbouw",
        "product_rturn": "veer"
    }];
    for (var i = 0; i < o.length; i++) {
        console.log("Product " + (i + 1) + " has model:" + o[i]["product_model"]);
        console.log("Product " + (i + 1) + " has type:" + o[i]["product_type"]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this php code: if ($merchant_rows > 0){ while($r=mysql_fetch_array($get_merchant)){ $merchant_id = $r['merchant_id']; }
We are stuck performing this mySQL query and the PHP along side it. Here
I'm writing server-side programs in PHP for an iPhone app. And I have no
I have a website which works with PHP on the server side. Users access
i have a rhel 4 server and use php as a server side scripting
i've been making web app's and working with various server side language like php,
If you wanted to re-create the DOM server-side and manipulate it in PHP, how
For example, writting server-side things using javascript, instend of php.
Client side sends post request to php on server side. Php returns json to
Quick question : I have a PHP script which query Pinnacle Cart API through

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.