Answer the question
In order to leave comments, you need to log in
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)
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]";
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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')); // итоговый набор данных
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question