How to get data from an array without using the Index

0

I am trying to get data through an API (bet365) it returns this data in JSON, I have created a function to extract this data and transform into array

The following function:        public function api_footer () {

  $url = "https://api.betsapi.com/v1/bet365/inplay_filter?sport_id=1&token=token&LNG_ID=22" . $this->refcode;


  $request_headers = array();
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $data = curl_exec($ch);

  if (curl_errno($ch))
    {
    print "Error: " . curl_error($ch);
    }
    else
    {
    // Show me the result

    $transaction = json_decode($data, TRUE);

    curl_close($ch);

   //var_dump($transaction);

   $dados = array();

   $dados['transaction'] = $transaction;
  }

       $this->load->view('header');
       $this->load->view('home', $dados);
       $this->load->view('footer');
}

JSON template I'm working on:

{
"success": 1,
"pager": {
    "page": 1,
    "per_page": 1000,
    "total": 4
},
"results": [
    {
        "id": "77564080",
        "time": "1543528800",
        "time_status": "1",
        "league": {
            "id": "3024",
            "name": "Copa Libertadores - Feminino"
        },
        "home": {
            "id": "9105",
            "name": "EC Iranduba - Feminino"
        },
        "away": {
            "id": "170148",
            "name": "Atlético Huila - Feminino"
        },
        "ss": "1-0",
        "our_event_id": "1093051"
    },

I'm showing the data in the view as follows:

   echo ($transaction['results'][0]['league']['name']);
   echo ($transaction['results'][0]['ss']);

I need to get this data without indicating the Index, I have tried the following ways:

     foreach ($transaction as $row){
     echo $row->name;
     }
     //ou
    foreach ($transaction as $row){
    echo $row->results['name'];
    }
    // ou
     foreach ($transaction as $row){
    echo $row->results['league']['name'];
     }

Note: I want without the index because I do not want to put the games manually that is automatically placed, for example: I have 10 games in the afternoon the way I'm doing I have to put 10 indexes but the night might have only 2 for example .

    
asked by anonymous 30.11.2018 / 00:29

1 answer

1

You're trying to access properties, which does not exist in an array, there are two ways to do what you need.

Form 1 - Use json as array (which is what you try to do but did not use right)

$transaction = json_decode($json, true);
foreach ($transaction['results'] as $element) {
    echo $element['league']['id'].PHP_EOL;
    echo $element['league']['name'].PHP_EOL;
}

Form 2 - Using json as an object

$transaction = json_decode($json);
foreach ($transaction->results as $element) {
    echo $element->league->id.PHP_EOL;
    echo $element->league->name.PHP_EOL;
}

Note that the second parameter of json_decode was passed in the first example and in the second example, this is because it is meant to transform your json objects into array. More information here

    
30.11.2018 / 14:11