如何创建一个多维数组PHP [英] how to create a multidimensional array PHP

查看:166
本文介绍了如何创建一个多维数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据在一个SQL数据库我的网站统计:

 日期:访问:浏览次数:
12-12-12 34 21
13年12月12日31 22
14年12月12日33 2445
15年12月12日35 2422
16年12月12日36 232//等等

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

 的print_r $ my_multi_array [12年12月5日];

和我会看到在屏幕上该日期的统计信息。

现在我知道该怎么做所有的循环和东西,我甚至有关于如何做多维数组是一个好主意,它只是我觉得我做错了什么:

  //首先第一件事情,定义数组:
$ my_multi_array =阵列();//然后,在一个循环中,追加到数组:
$my_multi_array[]=array(\"$date\"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));

现在,当我的print_r 该数组,一切都看起来很不错:

 阵列([0] =>数组([24年11月12日] =>数组([探访] => 1 [浏览量] = 0)) [1] =>阵列([25年11月12日] =>阵列([访问] =大于1 [浏览量] =&0))[2] =>阵列([26年11月12日] =>阵列([访问] =大于1 [浏览量] =大于0)))1

注意1末^^。这似乎是在结果(不是笔误)。

现在,当我尝试打印一定数组后(使用日期):

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

我得到:

  1

于是我尝试:

 的print_r $ my_multi_array [2];

和工作正常。

由于某些原因,它不会让我选择从 $ my_multi_array 使用日期为重点的数组。

这是如何解决这一问题的任何想法?

感谢


解决方案

您必须把日期作为数组键,就像这样:

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

注意 $ my_multi_array [$日期]

这样做 $ my_multi_array [] = ... 你是刚创建与右侧的内容阵列上的一种新的数值指标。这就是为什么当你与数值指标,如 $ my_multi_array访问数组[2] ,它的工作原理。

在另一方面,通过做 $ my_multi_array [$日期] 你对待像一个哈希表,在那里你一键关联数组(在这种情况下字符串包含的日期)的值。

I have some data for my sites statistics in a 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:

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.

Any ideas on how to fix this?

thanks

解决方案

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].

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.

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.

这篇关于如何创建一个多维数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆