0

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.

2 Answers 2

3

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);
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Manuel, while it certainly parses the code, it creates a singular array. I am trying to convert your sample code into an associative (multi-dimensional array).
I think that the only way to convert this on associative array is convert to object, but you need an array right? You can get an multidimensional positional array using aData [k] = [data[k].id, data[k].primary_authors, data[k].primary_titles, data[k].pub_year]; on foreach instead of my answer. If you get better solution let me know please.
It worked for me. Now I just have to add labels to the array so that ng-repeat can parse the data
0

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);

1 Comment

Thanks @PaoIm, I will certainly try to implement your solution as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.