0

I am trying to get a json result to an array object .I am echoing a json data in php. like this.

    <?php
header('Access-Control-Allow-Origin: *');
header('Content-type:application/json;charset=utf-8');
$arr=  '[{
   "id": "1",
   "date": "2020-03-21",
   "status": "present",
   "studentid": "1"
  },
  {
   "id": "2",
   "date": "2020-03-24",
   "status": "present",
   "studentid": "1"
  },
  {
   "id": "3",
   "date": "2020-03-25",
   "status": "absent",
   "studentid": "1"
  },
  {
   "id": "4",
   "date": "2020-03-26",
   "status": "absent",
   "studentid": "1"
  }
 ]';

echo $arr;
?>

~


How to get absentees using angular in an array.

Angular part i tried didnt work

      this.http.post("http://localhost/android/Api.php?apicall=getattendance", JSON.stringify(this.postData),options)


          .subscribe( (data) => {
          this.setUsersArray(data);
          console.log(data + "URL DATA"+JSON.stringify(this.postData));

           }


      );

=====================================================================
    setUsersArray(data){

       if (data instanceof Array) {
                       {

                        this.date_present = data.map(function (ele) {
                        if(ele.status==='present')
                        {

                          return ele.date;

                        }



                        });
                        this.date_absent = data.map(function (ele) {
                          if(ele.status==='absent')


                          return ele.date;

                          });
                   }
                    }

I am getting date_absent and date_present as null.Why am i getting this as null.Please help me. I am new to angular.

4
  • 1
    Looks like your if block is outside of the subscribe? So you probably don't have access to data. Commented Mar 6, 2020 at 9:33
  • actuaally that part is inside a function setUsersArray(data); Commented Mar 6, 2020 at 9:35
  • @sam sorry for that mistake.I have corrected it Commented Mar 6, 2020 at 9:36
  • Perhaps you don't want to use map for this, as map will return something for each item in the array you are looping over. So for this.date_present i would expect your data to look like ["2020-03-21", "2020-03-24", undefined, undefined]; You could use forEach instead to avoid this and push the values to the array this.date_present . Also your array looks like a string which is probably why you cannot use it at the moment, try throwing a debugger inside the if block and see if it is called. Commented Mar 6, 2020 at 9:47

2 Answers 2

1

try for loop in typescript

setUsersArray(data:any){
for(let item of data){
  console.log(item);
  if(item.status==='present') {
    this.present.push(item.date);
  }
  if(item.status==='absent') {
    this.absent.push(item.date);
  }       
}
Sign up to request clarification or add additional context in comments.

Comments

0

Trying to this url $characters = json_decode($data, true); // decode the JSON feed and make an associative array https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/

Comments

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.