CalmWinds
CalmWinds

Reputation: 157

How to convert ajax array of objects into array of arrays

I have an ajax call which returns array of objects like this:

enter image description here

Now, it works fine with my code of parsing it into a table in AngularJS.

I am trying to implement pagination with the table and issue arises that pagination does not work with objects, it has to be an array.

I am not able to convert this array of objects into array of arrays for pagination.

My Ajax call code is:

$http({
            method: 'POST',
            url: 'server/search/searchQuery.php',
            data: {data: 'getData', searchText: id.SearchText , typeOfStudy: qTypeOfStudy , typeOfSpecies: qTypeOfSpecies , typeOfSpeciality: qTypeOfSpeciality },
            headers: {'Content-Type': 'application/json'},
            dataType:"json"
        }).success(function(data) {

It would be great if someone can point to a post or a sample code which can convert it.

I already do json_encode on php side.

Upvotes: 0

Views: 2456

Answers (2)

ManuRGDev
ManuRGDev

Reputation: 535

Maybe you need this solution:

var aData = [];
    $http({
            method: 'POST',
            url: 'server/search/searchQuery.php',
            data: {data: 'getData', searchText: id.SearchText , typeOfStudy: qTypeOfStudy , typeOfSpecies: qTypeOfSpecies , typeOfSpeciality: qTypeOfSpeciality },
            headers: {'Content-Type': 'application/json'},
            dataType:"json"
        }).success(function(data) {
      var aData = [];
      for(var k in data){
        aData.push(data[k].id, data[k].primary_authors, data[k].primary_titles, data[k].pub_year);
        }
    }

Upvotes: 3

Pao Im
Pao Im

Reputation: 347

In PHP, try with:

$associateArray = array(
                'name'          => 'Test Data',
                'quantity'      => 1,
                'unit'          => 'ks',
                'unit_price'    => 200
        );

Convert Associated Array to Json Object

$JsonObject = json_encode($associateArray);

Convert Json String into Associated Array

$SociateArray = json_decode($JsonString, true);

Upvotes: 0

Related Questions