K
K
kanonir082020-05-01 14:19:09
PHP
kanonir08, 2020-05-01 14:19:09

Deriving nested array from multidimensional array?

Hello everyone, help to display a nested array of a multidimensional array if it satisfies the conditions

There is an array (I will display it for you with a picture to make it more clear)

5eac0551c424b751994140.png

Here is my code for outputting an array (there are still 2 levels of nesting above)

$json = json_decode($response);

// print_r($json);

foreach($json as $massivezero) {
    foreach($massivezero as $massiveone) {

 $parssport = $massiveone->sport_key;
 $parsleague = $massiveone->sport_nice;
 $parsteams = $massiveone->teams;   
 $bookmakers = $massiveone->sites;
     
$hometeam = $parsteams[0];
$awayteam = $parsteams[1];
 
 echo "<pre>";
 echo $parsleague.'<br/>';
 echo $hometeam.' - '; 
 echo $awayteam; 
 echo "</pre>";

       foreach($massiveone as $massivetwo) {
           foreach($massivetwo as $massivethree) {
 
               $bookiename = $massivethree->site_key;
 
             //Этот код выводит букмекерские конторы все
              echo "<pre>";
              print_r ($bookiename);
               echo "</pre>";               
                   foreach($massivethree as $massivefour) {
 $parsodds = $massivefour->h2h;
 // Выводим коэффициенты 1х2    
     echo "$parsodds[0] $parsodds[1] $parsodds[2]";
   
                    }      
               }
            }
        }
    }


Here is the output of the code.

5eac0579eca0b877000913.png

The code works, displays the match and all odds, but I don't need all of them.

How to write h2h coefficients for me only if site_key=unibet (screen 1)

I tried to do it through if, but I didn’t succeed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2020-05-01
@kanonir08

At the very bottom, I wrote - why you don’t need to write such code ...
Ready-made code for your task:

<?php

$input = '{"success":true,"data":[{"sport_key":"soccer_korea_kleague1","sport_nice":"K League 1","teams":["Jeonbuk Hyundai Motors","Suwon Samsung Bluewings"],"commence_time":1588932000,"home_team":"Jeonbuk Hyundai Motors","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1588281960,"odds":{"h2h":[1.75,3.85,3.7]}},{"site_key":"sport888","site_nice":"888sport","last_update":1588282210,"odds":{"h2h":[1.74,3.8,3.65]}},{"site_key":"onexbet","site_nice":"1xBet","last_update":1588282134,"odds":{"h2h":[1.87,3.96,3.5]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1588281947,"odds":{"h2h":[1.68,1.28,1.25],"h2h_lay":[85.0,85.0,85.0]}},{"site_key":"paddypower","site_nice":"Paddy Power","last_update":1588281989,"odds":{"h2h":[1.67,4.2,3.6]}}],"sites_count":5},{"sport_key":"soccer_korea_kleague1","sport_nice":"K League 1","teams":["Sangju Sangmu FC","Ulsan Hyundai FC"],"commence_time":1589000400,"home_team":"Ulsan Hyundai FC","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1588281960,"odds":{"h2h":[3.85,1.88,3.3]}},{"site_key":"sport888","site_nice":"888sport","last_update":1588282210,"odds":{"h2h":[3.8,1.85,3.25]}},{"site_key":"onexbet","site_nice":"1xBet","last_update":1588282134,"odds":{"h2h":[3.76,1.92,3.5]}}],"sites_count":3},{"sport_key":"soccer_korea_kleague1","sport_nice":"K League 1","teams":["Daegu FC","Incheon United"],"commence_time":1589009400,"home_team":"Incheon United","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1588281960,"odds":{"h2h":[2.23,2.95,3.2]}},{"site_key":"sport888","site_nice":"888sport","last_update":1588282210,"odds":{"h2h":[2.2,2.9,3.2]}},{"site_key":"onexbet","site_nice":"1xBet","last_update":1588282134,"odds":{"h2h":[2.28,2.98,3.3]}}],"sites_count":3},{"sport_key":"soccer_korea_kleague1","sport_nice":"K League 1","teams":["Sangju Sangmu FC","Seongnam FC"],"commence_time":1589018400,"home_team":"Sangju Sangmu FC","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1588281960,"odds":{"h2h":[2.38,2.75,3.2]}},{"site_key":"sport888","site_nice":"888sport","last_update":1588282210,"odds":{"h2h":[2.35,2.7,3.15]}}],"sites_count":2},{"sport_key":"soccer_korea_kleague1","sport_nice":"K League 1","teams":["FC Seoul","Gangwon FC"],"commence_time":1589095800,"home_team":"Gangwon FC","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1588281960,"odds":{"h2h":[2.38,2.75,3.2]}},{"site_key":"sport888","site_nice":"888sport","last_update":1588282210,"odds":{"h2h":[2.35,2.7,3.15]}},{"site_key":"onexbet","site_nice":"1xBet","last_update":1588282134,"odds":{"h2h":[2.42,2.84,3.2]}}],"sites_count":3}]}';
$data = json_decode($input, true);

function grabBookerData(array $data, string $bookerName): array {
    return array_map(function(array $item) use ($bookerName) {
        // Собираем результирующий объект события
        $event             = new \stdClass();
        $event->sport_nice = $item['sport_nice'] ?? null;
        $event->teams      = $item['teams'] ?? [];
    
        // Из данных по разным букмекерам оставляем h2h, который нам нужен
        $bookersData = array_column($item['sites'], null, 'site_key');
        $event->h2h  = $bookersData[$bookerName]['odds']['h2h'] ?? [];
        
        return $event;
    }, $data['data'] ?? []);
}

var_dump(grabBookerData($data, 'unibet')); // итоговый набор данных

sandbox.onlinephpfunctions.com/code/6cb94c1070692d...
Why you don't need to write such code:
It is better to pass such data through the serializer, and then just filter in the resulting objects (or just get) the necessary data.
The advantage of using serializers is that you don’t need to write the code above, but you only need to write your objects in the format you already need. And then the data is mapped onto these objects through serializers, thereby not getting entangled in the original data.
PHP serializers:
Fractal (recommended)
Symfony Serializer
Zend Serializer
JMS Serializer- many scold him for his slowness and monstrosity, but he is quite functional and takes on a bunch of body movements, I would do with him. It becomes a bit of a problem when tens and thousands of operations go into some unacceptable number of servers/time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question