如何创建一个多维数组PHP

时间:2022-08-27 18:04:27

I have some data for my sites statistics in a SQL DB:

我在SQL DB中有一些我的网站统计数据:

date:          visits:        pageviews:
12-12-12       34             21
12-12-13       31             22
12-12-14       33             2445
12-12-15       35             2422
12-12-16       36             232

//ect ect

I'm trying to create a Multidimensional array that contains all the dates info from the DB and where the date will be the key (selector,name of the array inside the multi array), so as the end result, I should be able to just do this:

我试图创建一个包含从数据库中所有的日期信息和其中的日期将是(多阵列内部的阵列的选择,名字)的关键多维数组,所以最后的结果,我应该能够这样做:

print_r $my_multi_array[12-05-12];

And I should see the statistics for that date on the screen.

我应该在屏幕上看到该日期的统计数据。

Now I know how to do all the loops and stuff, and I even have a good idea about how to do multidimensional arrays, it's just that I think I'm doing something wrong:

现在我知道如何做所有的循环和东西,我甚至对如何做多维数组有一个好主意,只是我认为我做错了什么:

//first things first, define the array:
$my_multi_array=array();

//then, in a loop, append to the array:
$my_multi_array[]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));

Now when I print_r that array, everything looks good:

现在,当我print_r那个数组时,一切看起来都很好:

Array ( [0] => Array ( [11-12-24] => Array ( [visits] => 1 [pageviews] => 0) ) [1] => Array ( [11-12-25] => Array ( [visits] => 1 [pageviews] => 0) ) [2] => Array ( [11-12-26] => Array ( [visits] => 1 [pageviews] => 0)))1

Notice the 1 at the end ^^. That seemed to be in the result (not a typo).

注意结尾的^ ^^。这似乎是结果(不是拼写错误)。

Now when I try printing a certain array out (using the date as the key):

现在,当我尝试打印某个数组时(使用日期作为键):

print_r $my_multi_array['11-12-24'];

I get:

1

So then i try:

那么我尝试:

print_r $my_multi_array[2];

and that works fine.

这很好。

For some reason, it wont let me select an array from $my_multi_array using the date as the key.

出于某种原因,它不会让我使用日期作为键从$ my_multi_array中选择一个数组。

Any ideas on how to fix this?

有想法该怎么解决这个吗?

thanks

2 个解决方案

#1


1  

You have to put the date as the array key, like so:

您必须将日期作为数组键,如下所示:

$my_multi_array[$date]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));

Notice the $my_multi_array[$date].

注意$ my_multi_array [$ date]。

By doing $my_multi_array[] = ... you are just creating a new numerical index on the array with the content on the right side. That's why when you access the array with the numerical index, like $my_multi_array[2], it works.

通过执行$ my_multi_array [] = ...您只是在数组上创建一个新的数字索引,其内容位于右侧。这就是为什么当您使用数字索引访问数组时,如$ my_multi_array [2],它可以工作。

On the other hand, by doing $my_multi_array[$date]you are treating the array like an hash table, where you associate a key (in this case a string containing a date) with a value.

另一方面,通过执行$ my_multi_array [$ date],您将数组视为哈希表,其中您将一个键(在本例中为包含日期的字符串)与值相关联。

#2


2  

Everything is correct, because you don't have array('key' => 'value') style array, instead of that you've got array( [0] => array( 'key' => 'value' ) ) that's why you're getting correct result on accessing numeric key of the array.

一切都是正确的,因为你没有数组('key'=>'value')样式数组,而不是你有数组([0] =>数组('key'=>'value'))这就是为什么你在访问数组的数字键时得到正确的结果。

#1


1  

You have to put the date as the array key, like so:

您必须将日期作为数组键,如下所示:

$my_multi_array[$date]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));

Notice the $my_multi_array[$date].

注意$ my_multi_array [$ date]。

By doing $my_multi_array[] = ... you are just creating a new numerical index on the array with the content on the right side. That's why when you access the array with the numerical index, like $my_multi_array[2], it works.

通过执行$ my_multi_array [] = ...您只是在数组上创建一个新的数字索引,其内容位于右侧。这就是为什么当您使用数字索引访问数组时,如$ my_multi_array [2],它可以工作。

On the other hand, by doing $my_multi_array[$date]you are treating the array like an hash table, where you associate a key (in this case a string containing a date) with a value.

另一方面,通过执行$ my_multi_array [$ date],您将数组视为哈希表,其中您将一个键(在本例中为包含日期的字符串)与值相关联。

#2


2  

Everything is correct, because you don't have array('key' => 'value') style array, instead of that you've got array( [0] => array( 'key' => 'value' ) ) that's why you're getting correct result on accessing numeric key of the array.

一切都是正确的,因为你没有数组('key'=>'value')样式数组,而不是你有数组([0] =>数组('key'=>'value'))这就是为什么你在访问数组的数字键时得到正确的结果。