How to take data from an array without using Index

Asked

Viewed 604 times

1

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

It follows function: public Function api_football(){

  $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 am showing the data in the view as follows:

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

I need to take this data without indicating which 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, example: I have 10 games in the afternoon the way I am doing I have to put 10 indexes but the night may be only 2 for example.

  • 1

    Have you ever tried to do: foreach($transaction['results'] as $row)?

  • Already! it gives the following message: trying to get Property 'Results' of non-object

  • 1

    You can give a var_dump($transaction) or var_dump($transaction->results)? If yes edit your question with the information

  • It comes as NULL

1 answer

1


You’re trying to access the properties, which doesn’t 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 didn’t 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 json_decode parameter has been passed in the first example and the second not, which is why it serves to say that you want to transform your json objects into an array. More information here

  • +1 Thank you! It worked

Browser other questions tagged

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