I can’t view all data with foreach in PHP with data from an API

Asked

Viewed 140 times

-2

I am trying to bring to a simpler way the following information coming from an API:

{
"results": [
    {
      "date": "2020-01-05T00:00:00Z",
      "total": 100,
      "visits_detail": [
      ]
    },
    {
      "date": "2020-01-06T00:00:00Z",
      "total": 200,
      "visits_detail": [
      ]
    },
    {
      "date": "2020-01-07T00:00:00Z",
      "total": 300,
      "visits_detail": [
      ]
    }
  ]
}

What I want to do is turn this part of the code into something like this, turning the date from ISO8601 Standard to Datetime, so I won’t have trouble displaying the data with Apexchart.

{
"results": [
    {
      "date": "05-01-2020",
      "total": 100,
      "visits_detail": [
      ]
    },
    {
      "date": "06-01-2020"",
      "total": 200,
      "visits_detail": [
      ]
    },
    {
      "date": "07-01-2020"",
      "total": 300,
      "visits_detail": [
      ]
    }
  ]
}

Turns out I stalled and I can’t get all the information accessed inside the foreach. That’s my code:

foreach ($myArray2['results'] as $key => $value)
{
  $data1 = date('d-m-Y', strtotime($myArray2['results'][$key]['date']));
  $data2 = $myArray2['results'][$key]['total']; 
}
$arrayfinal = array('results'=> (array('date'=>$data1,'total'=>$data2)));
echo json_encode($arrayfinal);

And the answer I get is just this, I can’t display all the items, and it’s not array format.

{
"results": {
"date": "07-01-2020",
"total": 300
}
}

3 answers

3


Note that inside the foreach, there are two variables $data1 and $data2, they will store the last information that was iterated in the loop, as it is a simple variable.

If you want to change the date directly in the array is possible, follow an example.

<?php 
$myArray2= [];

$myArray2["results"][0]["date"] =  "2020-01-05T00:00:00Z";
$myArray2["results"][1]["date"] =  "2020-01-05T00:00:00Z";
$myArray2["results"][2]["date"] =  "2020-01-05T00:00:00Z";


var_dump($myArray2);

foreach ($myArray2['results'] as $key => $value) {
    $myArray2['results'][$key] = date('d-m-Y', strtotime($myArray2['results'][$key]['date']));;
}

var_dump($myArray2);

The result is the same, but it is treating the variable directly in the same array.

VAR_DUMP-1 = array(1) { ["results"]=> array(3) { [0]=> array(1) { ["date"]=> string(20) "2020-01-05T00:00:00Z" } [1]=> array(1) { ["date"]=> string(20) "2020-01-05T00:00:00Z" } [2]=> array(1) { ["date"]=> string(20) "2020-01-05T00:00:00Z" } } } 
VAR_DUMP-2 = array(1) { ["results"]=> array(3) { [0]=> array(1) { ["date"]=> string(10) "04-01-2020" } [1]=> array(1) { ["date"]=> string(10) "04-01-2020" } [2]=> array(1) { ["date"]=> string(10) "04-01-2020" } } }
  • How I can insert in the same array the Total of visits on each date?

1

1

I was able to solve by creating a new array with the results:

foreach ($myArray2['results'] as $key => $value) {


    $myArray2['results'][$key] = array('date' => date('d-m-Y', strtotime($myArray2['results'][$key]['date'])),'total' =>$myArray2['results'][$key]['total']);

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.