PHP/Laravel, foraeach: ($array as $value) works, ($array as $key => $value) does not?! -
i have no idea how explain question in way giving example. hope ok.
i defined 2 arrays:
$range = ['1000' => '1100', '1100' => '1200', '1200' => '1300'];
and
$years = ['2010', '2011'];
then try data given database like:
foreach ($years $year) { foreach ($range $from => $to) { $result[$year][$from] = flight::leftjoin('aircrafts', 'flights.lfz_lfdnr', '=', 'aircrafts.lfz_lfdnr') ->selectraw('aircrafts.gewicht, sum(flights.anzldg) landungen') ->whereyear('datum', '==', $year) ->where('gewicht', '>=', $from) ->where('gewicht', '<', $to) ->count(); }; };
which works output like:
{ "2010": { "1000": 821, "1100": 979, "1200": 126, "1300": 127, "1400": 69, "1500": 157, "1600": 33, "1700": 364, "1800": 64, "1900": 69 }, "2011": { "1000": 891, "1100": 1027, "1200": 112, "1300": 128, "1400": 76, "1500": 135, "1600": 64, "1700": 701, "1800": 96, "1900": 67 }
}
i changed $years array to:
$years = ['2010'=>'red','2011'=>'green'];
and query to:
foreach ($years $year => $color) { foreach ($range $from => $to) { $result[$year][$from] = flight::leftjoin('aircrafts', 'flights.lfz_lfdnr', '=', 'aircrafts.lfz_lfdnr') ->selectraw('aircrafts.gewicht, sum(flights.anzldg) landungen') ->whereyear('datum', '==', $year) ->where('gewicht', '>=', $from) ->where('gewicht', '<', $to) ->count(); }; };
what got:
{ "2010": { "1000": 0, "1100": 0, "1200": 0, "1300": 0, "1400": 0, "1500": 0, "1600": 0, "1700": 0, "1800": 0, "1900": 0 }, "2011": { "1000": 0, "1100": 0, "1200": 0, "1300": 0, "1400": 0, "1500": 0, "1600": 0, "1700": 0, "1800": 0, "1900": 0 }
}
and have no idea why "0"! when try debug , check $year var, still has correct value. if insert e.g. '2010' instead of $year in ->whereyear(), works well. have no idea causes problem. welcome.
many thanks!
Comments
Post a Comment