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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:20:27+00:00 2026-05-31T15:20:27+00:00

I am having this issue I have been struggling with for sometime and now

  • 0

I am having this issue I have been struggling with for sometime and now I need help:
I have the following array

array(2) {
  [0]=>
  object(stdClass)#4 (4) {
    ["id"]=>
    string(1) "1"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(6) "Legend"
    ["modelYear"]=>
    string(4) "1986"
  }
  [1]=>
  object(stdClass)#5 (4) {
    ["id"]=>
    string(1) "2"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(3) "MDX"
    ["modelYear"]=>
    string(4) "2000"
  }
}

How can I use it the $.get() function via jQuery in order to have something like this:

id   Model      Year
  1    Legend     1986
  2    MDX        2000

I have already tried the following:

a process.php file:

<?php
require 'DataLayer.class.php';
$dl = new DataLayer();

//get car make models
$models = $dl->getCarModels($id);
if(isset($models)){
    echo json_encode(json_encode($models));
}
else{
    echo 'failed';
}
?>

the getCarModels function:

public function  getCarModels($id){
    $stmt = $this->pdo->prepare("SELECT  * FROM model WHERE idMake=? ORDER BY modelName");
    $stmt->execute(array($id));
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}

Javascript function called upon clicking on some links

function getCarModels(id, make){
    $.get(process.php, function(data){       
        var models = $.parseJSON(data); 
        for(var model in models.model.modelName){ // I got stuck here

        } 
    });
} 

Hope this makes any sense to someone. Thanks.

Just find out some similar issue at this link: php multidimensional array into jQuery I am giving a try.


Like I said from the start, my issue is quite similar to the one posted at this link php multidimensional array into jQuery. My only question is that I really don’t know the reason why they use the json_encode() function twice as all I did to make it work properly is to remove one of the json_encode() functions.

Will appreciate some explanation of the reason why I used only jsan_encode() function to get the result I wanted while in most tutorial it has been two time before outputting the data.

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-31T15:20:30+00:00Added an answer on May 31, 2026 at 3:20 pm

    You don’t need to use parseJSON within jQuery ajax, jQuery will already handle that. Your data is an array of objects. Here’s example to loop over it with $.each

    Your getCarModels() function isn’t set up to send data

    function getCarModels(id, make){
    
         var dataToServer={ id: id, make: make};/* need to match these keys to $_GET keys in php*/
        $.get(process.php, dataToServer, function(data){       
            $.each( data, function(i, item){
                $('body').append('<p>'+item.modelName+'</p>');
            })
        });
    }
    

    The php has issues also , it doesn’t appear you are looking for $_GET from the ajax to pass to your query methods. I don’t recognize the framework functions used in your php but to pass the ID to getCarModels in pphp you would need something like:

     $id=$_GET['id'];
     //get car make models
     $models = $dl->getCarModels($id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having this issue in iterating through an array of keys and
I have been having this issue for a while now and I cannot seem
We have been having this issue pop up sporadically, but now I can reproduce
i have been having this issue for some time now, and have not gotten
Good morning. I've been having this issue for some days and have been lokking
I am having trouble with the following SQL statement. I have had this issue
I have been having this problem for some time now, I dont exactly know
I have been having this issue with blurry text in WPF. I know that
I have been trying to figure out this issue I am having with PHP
I've been having this issue for a while now and was never really able

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.