Foreach stops before end and provides unexpected results

<?php
 
$thedata = json_decode('{"matches":[{"region":"EUW","platformId":"EUW1","matchId":2414306652,"champion":62,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449179576202,"lane":"TOP","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2413755928,"champion":18,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449152221268,"lane":"BOTTOM","role":"DUO"},{"region":"EUW","platformId":"EUW1","matchId":2413208629,"champion":62,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449101306636,"lane":"MID","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2412969151,"champion":104,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449088159103,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2412888295,"champion":104,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449084268991,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2412592377,"champion":104,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449069366079,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2412527121,"champion":104,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449066716625,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2412126072,"champion":18,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1449013238068,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2411526152,"champion":21,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1448993141856,"lane":"BOTTOM","role":"DUO"},{"region":"EUW","platformId":"EUW1","matchId":2409915378,"champion":103,"queue":"RANKED_SOLO_5x5","season":"PRESEASON2016","timestamp":1448850659123,"lane":"MID","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2348538107,"champion":62,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1445299301233,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2342956527,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1445039820883,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2341031310,"champion":203,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444938456224,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2340171319,"champion":203,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444919067318,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2340110625,"champion":203,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444916417795,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2340074627,"champion":11,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444914256575,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2339810659,"champion":103,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444865904704,"lane":"MID","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2338551366,"champion":28,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444818881287,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2337969105,"champion":91,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444778119532,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2336967841,"champion":67,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444746332213,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2336657302,"champion":62,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444692464215,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2336515883,"champion":119,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444682338471,"lane":"BOTTOM","role":"DUO"},{"region":"EUW","platformId":"EUW1","matchId":2336182026,"champion":11,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444669545971,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2335568128,"champion":21,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444657521604,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2335106647,"champion":104,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444596963095,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2334746428,"champion":79,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444581622110,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2334092525,"champion":39,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444570664517,"lane":"TOP","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2333560014,"champion":79,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444515647633,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2333518761,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444513579521,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2332169066,"champion":11,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444469295633,"lane":"JUNGLE","role":"NONE"},{"region":"EUW","platformId":"EUW1","matchId":2332107867,"champion":91,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444457754100,"lane":"MID","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2332103510,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444452902839,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2331458629,"champion":51,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444423798922,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2331398823,"champion":67,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444421711184,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2330888190,"champion":51,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444400310681,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2330834323,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444397100814,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2330140311,"champion":51,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444345235796,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2330088469,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444340458166,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2330008344,"champion":98,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444336149136,"lane":"TOP","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2329936773,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444333835734,"lane":"BOTTOM","role":"DUO"},{"region":"EUW","platformId":"EUW1","matchId":2329180514,"champion":81,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444258997998,"lane":"MID","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2328757062,"champion":117,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444255879290,"lane":"TOP","role":"SOLO"},{"region":"EUW","platformId":"EUW1","matchId":2328121101,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444224467988,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2327865572,"champion":42,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444186815723,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2327861649,"champion":117,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444183188347,"lane":"BOTTOM","role":"DUO_SUPPORT"},{"region":"EUW","platformId":"EUW1","matchId":2327835579,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444179012396,"lane":"BOTTOM","role":"DUO_CARRY"},{"region":"EUW","platformId":"EUW1","matchId":2327464038,"champion":18,"queue":"RANKED_SOLO_5x5","season":"SEASON2015","timestamp":1444164167259,"lane":"BOTTOM","role":"DUO_CARRY"}],"startIndex":0,"endIndex":47,"totalGames":1220}', true);

echo "<pre>";
//var_dump($thedata);
					   
					   
$matchIndex = 0;
$toget = array();

foreach ($thedata as $match) {
	$toget[$matchIndex]["matchId"] = $match[$matchIndex]["matchId"];
	$matchIndex++;
}

var_dump($toget);
					   
					   
?>

To begin, it provides 4 answers in an array, JSON is way bigger than that, should provide at least 25. And only one of the four results given is correct, rest is empty.

I linked PHP above.

What am I doing wrong?

I changed this line

foreach ($thedata as $match) {

to this

foreach ($thedata['matches'] as $match) {

and then

$toget[$matchIndex]["matchId"] = $match["matchId"];

and it worked OK. Possibly because $thedata contains the matches array, and some individual variables at the end. I think your main foreach() iterates through the matches array and then the three other variables, rather than iterating through the matches array on its own. Someone will probably have a better explanation.

1 Like

Hi zvdifskt welcome to the forum

Yes, that’s it. To get what you want you need to go to where it is. To do that you need to know what you are working with.

The answer lies in what was commented out. i.e.

//var_dump($thedata);

Removing the comment you can see

array(4) {
  ["matches"]=>
  array(47) {
    [0]=>
    array(9) {
⋮
    }
  }
  ["startIndex"]=>
  int(0)
  ["endIndex"]=>
  int(47)
  ["totalGames"]=>
  int(1220)
}

That is, $thedata has 4 members.
the matches Array with 47 members.
and the three Int variables

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.