Reputation: 1944
I have a function:
function getTrend($results, $region, $monthNo)
{
$dateMap = getLastNMonths(6);
$keys = array_keys($dateMap);
$val = $results[$region][$keys[$monthNo]];
if(isset($val))
{
return round($val, 2);
}
else {
return '0.00';
}
}
..where $val is returning NULL. Now I know there is data in $dateMap and $keys from doing a var_dump() on them...
Here is a couple lines from $results:
array(21) { ["01"]=> array(5) { ["01-SEP-11"]=> string(40) "596.386666666666666666666666666666666667" ["01-OCT-11"]=> string(40) "639.347666666666666666666666666666666667" ["01-NOV-11"]=> string(40) "857.364833333333333333333333333333333333" ["01-DEC-11"]=> string(40) "663.022833333333333333333333333333333333" ["01-JAN-12"]=> string(41) "1094.066833333333333333333333333333333333" } ["02"]=> array(5) { ["01-SEP-11"]=> string(8) "510.1675" ["01-OCT-11"]=> string(8) "542.4725" ["01-NOV-11"]=> string(8) "809.4245" ["01-DEC-11"]=> string(40) "314.023833333333333333333333333333333333" ["01-JAN-12"]=> string(40) "913.979666666666666666666666666666666667" } ["03"]=> array(5) { ["01-SEP-11"]=> string(40) "594.528333333333333333333333333333333333" ["01-OCT-11"]=> string(40) "465.415333333333333333333333333333333333" ["01-NOV-11"]=> string(40) "508.904666666666666666666666666666666667" ["01-DEC-11"]=> string(8) "355.6895" ["01-JAN-12"]=> string(7) "706.928" } ["04"]=> array(5) { ["01-SEP-11"]=> string(40) "112.094833333333333333333333333333333333" ["01-OCT-11"]=> string(40) "399.436666666666666666666666666666666667" ["01-NOV-11"]=> string(40) "193.798166666666666666666666666666666667" ["01-DEC-11"]=> string(40) "246.241666666666666666666666666666666667" ["01-JAN-12"]=> string(40) "582.542333333333333333333333333333333333" } ["WOCB"]=> array(5) { ["01-SEP-11"]=> string(40) "216.045166666666666666666666666666666667" ["01-OCT-11"]=> string(40) "139.774333333333333333333333333333333333" ["01-NOV-11"]=> string(7) "254.421" ["01-DEC-11"]=> string(7) "142.355" ["01-JAN-12"]=> string(40) "119.464666666666666666666666666666666667" } ["NONE"]=> array(5) { ["01-SEP-11"]=> string(7) "427.196" ["01-OCT-11"]=> string(40) "430.996333333333333333333333333333333333" ["01-NOV-11"]=> string(40) "259.113666666666666666666666666666666667" ["01-DEC-11"]=> string(40) "188.372166666666666666666666666666666667" ["01-JAN-12"]=> string(40) "332.743333333333333333333333333333333333" } }
Here is a couple lines from $dateMap:
array(6) { ["01-SEP-2011"]=> string(14) "September 2011" ["01-OCT-2011"]=> string(12) "October 2011" ["01-NOV-2011"]=> string(13) "November 2011" ["01-DEC-2011"]=> string(13) "December 2011" ["01-JAN-2012"]=> string(12) "January 2012" ["01-FEB-2012"]=> string(13) "February 2012" } array(6) { ["01-SEP-2011"]=> string(14) "September 2011" ["01-OCT-2011"]=> string(12) "October 2011" ["01-NOV-2011"]=> string(13) "November 2011" ["01-DEC-2011"]=> string(13) "December 2011" ["01-JAN-2012"]=> string(12) "January 2012" ["01-FEB-2012"]=> string(13) "February 2012" } array(6) { ["01-SEP-2011"]=> string(14) "September 2011" ["01-OCT-2011"]=> string(12) "October 2011" ["01-NOV-2011"]=> string(13) "November 2011" ["01-DEC-2011"]=> string(13) "December 2011" ["01-JAN-2012"]=> string(12) "January 2012" ["01-FEB-2012"]=> string(13) "February 2012" }
Here is a couple lines from $keys:
array(6) { [0]=> string(11) "01-SEP-2011" [1]=> string(11) "01-OCT-2011" [2]=> string(11) "01-NOV-2011" [3]=> string(11) "01-DEC-2011" [4]=> string(11) "01-JAN-2012" [5]=> string(11) "01-FEB-2012" } array(6) { [0]=> string(11) "01-SEP-2011" [1]=> string(11) "01-OCT-2011" [2]=> string(11) "01-NOV-2011" [3]=> string(11) "01-DEC-2011" [4]=> string(11) "01-JAN-2012" [5]=> string(11) "01-FEB-2012" } array(6) { [0]=> string(11) "01-SEP-2011" [1]=> string(11) "01-OCT-2011" [2]=> string(11) "01-NOV-2011" [3]=> string(11) "01-DEC-2011" [4]=> string(11) "01-JAN-2012" [5]=> string(11) "01-FEB-2012" } array(6) { [0]=> string(11) "01-SEP-2011" [1]=> string(11) "01-OCT-2011" [2]=> string(11) "01-NOV-2011" [3]=> string(11) "01-DEC-2011" [4]=> string(11) "01-JAN-2012" [5]=> string(11) "01-FEB-2012" } array(6) { [0]=> string(11) "01-SEP-2011" [1]=> string(11) "01-OCT-2011" [2]=> string(11) "01-NOV-2011" [3]=> string(11) "01-DEC-2011" [4]=> string(11) "01-JAN-2012" [5]=> string(11) "01-FEB-2012" }
$monthNo can equal either 0,1,2,3,4,5. For example:
$mo2 = getTrend($results, $region, 1);
My question is, why can I not see anything from $val? What am I missing here? Did I show you enough code? Thanks :)
Upvotes: 1
Views: 5980
Reputation: 492
The problem is you have an associative array and you want to access it like an indexed array.
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays
You cannot access a value of an associative array like
echo $results[1];
because you have strings in the keys of your array. $results["01"]
Well, what happens when you do not know the key of your array and its being generated dynamically?
If you do not know the key to the array, you will have to use array_values
. This function takes an array and spits back out an array, but the return array is indexed 0,1,2... the keys are lost but the values are not.
To get the value of an array at index 1 of an associative array
echo array_values($results)[1]
Upvotes: 2
Reputation: 127
"result" return null, index 01 != 1
var_dump(isset($val)); // false
Upvotes: 0
Reputation: 1112
See this part:
array(21) { ["01"]=> array(5) { ["01-SEP-11"]=> string(40) ...
Look at the key - it's "01"
in quotes. That means it's a string, so you would have to do
$results["01"]
to get at that key. If you're passing in
$results[1]
It's not going to show up. When you initially populate $results
that key should be cast it to an int
Upvotes: 2