数组返回 NULL,但我可以看到数据 var_dump(....)。参见代码

发布于 2025-01-06 08:28:11 字数 4628 浏览 4 评论 0原文

我有一个函数:

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';
    }
}

..其中 $val 返回 NULL。现在我知道 $dateMap 和 $keys 中有数据,是通过对它们执行 var_dump() 得到的...

这是来自 $results 的几行:

数组(21) { ["01"]=>数组(5) { ["01-SEP-11"]=>;字符串(40) “596.3866666666666666666666666666666666667”[“01-OCT-11”]=>字符串(40) “639.3476666666666666666666666666666666667”[“01-NOV-11”]=>字符串(40) “857.364833333333333333333333333333333333”[“01-DEC-11”]=>字符串(40) “663.022833333333333333333333333333333333”[“01-JAN-12”]=>字符串(41) "1094.066833333333333333333333333333333333" } ["02"]=>数组(5){ [“01-SEP-11”] =>字符串(8)“510.1675”[“01-OCT-11”]=>字符串(8) “542.4725”[“01-NOV-11”]=>字符串(8)“809.4245”[“01-DEC-11”]=> 字符串(40)“314.023833333333333333333333333333333333”[“01-JAN-12”]=> 字符串(40) "913.9796666666666666666666666666666666667" } ["03"]=> 数组(5) { ["01-SEP-11"]=>;字符串(40) “594.528333333333333333333333333333333333”[“01-OCT-11”]=>字符串(40) “465.415333333333333333333333333333333333”[“01-NOV-11”]=>字符串(40) “508.904666666666666666666666666666666667”[“01-DEC-11”]=>字符串(8) "355.6895" ["01-JAN-12"]=>字符串(7) "706.928" } ["04"]=>数组(5){ [“01-SEP-11”] =>字符串(40)“112.094833333333333333333333333333333333” [“2011 年 10 月 1 日”]=>字符串(40)“399.436666666666666666666666666666666667” [“2011 年 11 月 1 日”]=>字符串(40)“193.798166666666666666666666666666666667” [“01-DEC-11”] =>字符串(40)“246.241666666666666666666666666666666667” [“01-JAN-12”] =>字符串(40)“582.542333333333333333333333333333333333” } [“WOCB”]=>数组(5) { ["01-SEP-11"]=>;字符串(40)“216.045166666666666666666666666666666667”[“01-OCT-11”]=>字符串(40)“139.774333333333333333333333333333333333”[“01-NOV-11”]=>字符串(7)“254.421”[“01-DEC-11”]=>字符串(7)“142.355”[“01-JAN-12”]=>字符串(40) "119.464666666666666666666666666666666667" } ["NONE"]=>数组(5) { ["01-SEP-11"]=>;字符串(7)“427.196”[“01-OCT-11”]=>字符串(40)“430.996333333333333333333333333333333333”[“01-NOV-11”]=>字符串(40)“259.113666666666666666666666666666666667”[“01-DEC-11”]=>字符串(40)“188.372166666666666666666666666666666667”[“01-JAN-12”]=>字符串(40)“332.743333333333333333333333333333333333”}}

这是 $dateMap 中的几行:

数组(6) { ["01-SEP-2011"]=>字符串(14)“2011 年 9 月” [“2011 年 10 月 1 日”]=> string(12) "2011 年 10 月" ["01-NOV-2011"]=> string(13) "2011 年 11 月" ["01-DEC-2011"]=>字符串(13)“十二月 2011" ["01-JAN-2012"]=> 字符串(12) "2012 年 1 月" ["01-FEB-2012"]=> 字符串(12) string(13) "2012 年 2 月" } array(6) { ["01-SEP-2011"]=>字符串(14) “2011 年 9 月”[“01-OCT-2011”]=>字符串(12)“2011 年 10 月” [“2011 年 11 月 1 日”]=> string(13) "2011 年 11 月" ["01-DEC-2011"]=> string(13) "2011 年 12 月" ["01-JAN-2012"]=>字符串(12)“2012 年 1 月” [“2012 年 2 月 1 日”]=>字符串(13) "2012 年 2 月" } 数组(6) { [“2011 年 9 月 1 日”]=> string(14) "2011 年 9 月" ["01-OCT-2011"]=> string(12) "2011 年 10 月" ["01-NOV-2011"]=>字符串(13)“2011 年 11 月” [“2011 年 12 月 1 日”]=> string(13) "2011 年 12 月" ["01-JAN-2012"]=> string(12) "2012 年 1 月" ["01-FEB-2012"]=>字符串(13)“2012 年 2 月” }

是 $keys 中的几行:

数组(6) { [0]=>;字符串(11)“2011 年 9 月 1 日”[1] =>字符串(11) “2011 年 10 月 1 日”[2]=>字符串(11)“2011 年 11 月 1 日”[3] =>字符串(11) “2011 年 12 月 1 日”[4]=>字符串(11)“2012 年 1 月 1 日”[5]=>字符串(11) "2012 年 2 月 1 日" } array(6) { [0]=>字符串(11)“2011 年 9 月 1 日”[1] => 字符串(11)“2011 年 10 月 1 日”[2]=>字符串(11)“2011 年 11 月 1 日”[3] => 字符串(11)“2011 年 12 月 1 日”[4]=>字符串(11)“2012 年 1 月 1 日”[5]=> 字符串(11) "01-FEB-2012" } 数组(6) { [0]=>字符串(11)“2011 年 9 月 1 日” [1]=>字符串(11)“2011 年 10 月 1 日”[2]=>字符串(11)“2011 年 11 月 1 日”[3] => 字符串(11)“2011 年 12 月 1 日”[4]=>字符串(11)“2012 年 1 月 1 日”[5]=> 字符串(11) "01-FEB-2012" } 数组(6) { [0]=>字符串(11)“2011 年 9 月 1 日” [1]=>字符串(11)“2011 年 10 月 1 日”[2]=>字符串(11)“2011 年 11 月 1 日”[3] => 字符串(11)“2011 年 12 月 1 日”[4]=>字符串(11)“2012 年 1 月 1 日”[5]=> 字符串(11) "01-FEB-2012" } 数组(6) { [0]=>字符串(11)“2011 年 9 月 1 日” [1]=>字符串(11)“2011 年 10 月 1 日”[2]=>字符串(11)“2011 年 11 月 1 日”[3] => 字符串(11)“2011 年 12 月 1 日”[4]=>字符串(11)“2012 年 1 月 1 日”[5]=> 字符串(11)“2012 年 2 月 1 日”}

$monthNo 可以等于 0,1,2,3,4,5。例如:

$mo2 = getTrend($结果, $region, 1);

我的问题是,为什么我看不到 $val 中的任何内容?我在这里缺少什么?我向您展示了足够的代码吗?谢谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2025-01-13 08:28:11

请参阅这部分:

array(21) { ["01"]=> array(5) { ["01-SEP-11"]=> string(40) ...

查看关键 - 它是引号中的 "01"。这意味着它是一个字符串,因此您必须

$results["01"]

获取该键。如果你传入

$results[1]

它就不会出现。当您最初填充 $results 时,该键应将其转换为 int

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

话少情深 2025-01-13 08:28:11

问题是您有一个关联数组,并且您希望像索引数组一样访问它。

在 PHP 中,数组分为三种类型:

索引数组 - 具有数字索引的数组

关联数组 - 具有命名键的数组

多维数组 - 包含一个或多个数组的数组

有关数组的更多信息

的更多信息 您无法访问 这样的关联数组

echo $results[1];

,因为数组的键中有字符串。 $results["01"]

那么,当您不知道数组的键及其动态生成时会发生什么?

如果您不知道数组的键数组,您必须使用array_values。该函数接受一个数组并吐出一个数组,但返回数组的索引为 0,1,2...键丢失了,但值没有丢失。

获取关联数组索引 1 处的数组值

echo array_values($results)[1]

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

More about 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]

当梦初醒 2025-01-13 08:28:11

“结果”返回 null,索引 01 != 1

var_dump(isset($val)); // false

"result" return null, index 01 != 1

var_dump(isset($val)); // false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文