ncf
ncf

Reputation: 556

Formatting json object using php

I'm trying to encode a json response using php, and having some trouble formatting it for use with ajax.

I'm basically trying to return an array of Rental objects, which each will include data for book, student, and teacher. Currently I'm using php to build objects like this...

while ($row = $result->fetch_array(MYSQLI_BOTH)) {
        $obj = array();

        // Build a book out of the results array, then push to the current object
        $book = new Book();
        $book->id = $row['book_id'];
        $book->title = $row['title'];
        $book->author = $row['author'];
        $book->ar_quiz = $row['ar_quiz'];
        $book->ar_quiz_pts = $row['ar_quiz_pts'];
        $book->book_level = $row['book_level'];
        $book->type = $row['type'];
        $book->teacher_id = $row['teacher_id'];
        array_push($obj, array('book' => $book));

        // Build a student out of the results array, then push it to the current objects
        $student = new Student();
        $student->id = $row['student_id'];
        $student->username = $row['student_username'];
        $student->nicename = $row['student_nicename'];
        $student->classroom_number = $row['classroom_number'];
        array_push($obj, array('student' => $student));


        // Build a teacher out of the results, push to current object
        $teacher = new Teacher();
        $teacher->id = $row['teacher_id'];
        $teacher->username = $row['teacher_username'];
        $teacher->nicename = $row['teacher_nicename'];
        array_push($obj, array('teacher' => $teacher));

        array_push($rentals, $obj);
    }

    mysqli_stmt_close($stmt);
    return json_encode($rentals);

... building an $obj for each result, and then appending the whole $obj object to the end of $rentals, which is what I pass back in the end. Here is what the response looks like when I encode it to json:

   [  
      [  
        {  
           "book":{  
              "id":113,
              "title":"Book Test",
              "author":"Test Test Author",
              "ar_quiz":1,
              "ar_quiz_pts":"10.0",
              "book_level":"20.0",
              "type":"Fiction",
              "teacher_id":1
           }
        },
      {  
           "student":{  
              "id":2,
              "username":"studentnametest",
              "classroom_number":2,
              "nicename":"Student Name"
           }
      },
    ],
    ...
  ]

The problem here is that there is an extra {} around each of the book, student, and teacher objects, causing an extra step when trying to access in the javascript. For example, I think I have to use data[0].[0].book.title, when I really just want to be able to use data[0].book.title. How do I better structure this to fit my needs?

Upvotes: 1

Views: 45

Answers (1)

chandlermania
chandlermania

Reputation: 360

Don't add the additional array structure and you can simply change your array_push lines from

array_push($obj, array('book' => $book));

to

$obj['book'] =  $book;

Upvotes: 3

Related Questions