How to loop through an array in Ionic controller (Ionic3 framework)


#1

Hi everyone, please help me out here .
I am working on a quiz app, wherein I have used REST API to return the data from database. But as the data is too huge am unable to fetch it all in JSON format and hence am fetching it in the form of php array as shown below.
The problem is am really unaware of how to use this in the typescript file and loop thru in the controller. Am stuck with this since past 3-4 days.Any help or ideas will be appreciated, thanks in advance.

My sample array:

     Array
         (
          [0] => Array
          (
           [SubjectID] => 16
           [QuestionID] => 4358
           [QuestionType] => 
           [Question] => Which one is a wrong statement?
           [Answer1] => some answer1.
           [Answer2] => some answer2
           [Answer3] => some answer3
            [Answer4] => some answer4
            [CorrectAnswer] => 4
             [Hint] => 
             [DiffLevel] => Medium
             [Status] => Active
            [AnsDescription] => some description. 
             )

       [1] => Array
       (
        [SubjectID] => 16
        [QuestionID] => 4359
        [QuestionType] => SingleAnswer
        [QuestionType] => 
        [Question] => Which one is a wrong statement?
        [Answer1] => some answer1.
        [Answer2] => some answer2
        [Answer3] => some answer3
        [Answer4] => some answer4
        [CorrectAnswer] => 4
        [Hint] => 
        [DiffLevel] => Medium
        [Status] => Active
        [AnsDescription] => some description
       )

    )

My controller/ typescript file:
Can I use it like below or should I assign it the “items” variable in some other way (Note: am assigning the data here directly to just test if its possible to loop through it and display in the html file).
Am a newbie to Ionic , so please excuse me if my question is something stupid.
import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;

_Component({
selector: 'page-home',
 templateUrl: 'home.html'
 })
 export class HomePage {

 items : any [];
 constructor(public navCtrl: NavController) {

 }

  this.items = [Array
  (
  [0] => Array
    (
    [SubjectID] => 16
    [QuestionID] => 4358
    [QuestionType] => 
    [Question] => Which one is a wrong statement?
    [Answer1] => some answer1.
    [Answer2] => some answer2
    [Answer3] => some answer3
    [Answer4] => some answer4
    [CorrectAnswer] => 4
    [Hint] => 
    [DiffLevel] => Medium
    [Status] => Active
    [AnsDescription] => some description. 
   )

   [1] => Array
   (
    [SubjectID] => 16
    [QuestionID] => 4359
    [QuestionType] => SingleAnswer
    [QuestionType] => 
    [Question] => Which one is a wrong statement?
    [Answer1] => some answer1.
    [Answer2] => some answer2
    [Answer3] => some answer3
    [Answer4] => some answer4
    [CorrectAnswer] => 4
    [Hint] => 
    [DiffLevel] => Medium
    [Status] => Active
    [AnsDescription] => some description
  )

  )]
  }

#2

This makes no sense to me. How is this format significantly smaller than JSON? My advice: use JSON. Don’t reinvent this wheel and write parsers for some other format.


#3

Hey @rapropos, thanks for that. I already tried using json, but when i use echo $json_encode($data), it gives me this error.
Malformed UTF-8 characters, possibly incorrectly encoded


#4

Somebody that doesn’t hate PHP as much as me will have to address that. Perhaps you need to base64 encode something.


#5

Can you give me an example of how could I use base64, aren’t JSON encoding and base64 different?