php动态创建多维数组

时间:2013-07-17 19:10:01

标签: php dynamic multidimensional-array

我正在编写一个PHP脚本来从MYSQL数据库中提取一些数据,然后我想获取该数据并将其存储到二维数组中。正常思考,如果我使用这样的代码

$test = array();
$test[0]=array();
$test[0]['hello']='hello';
$test[1]=array();
$test[1]['hello']='hello';
$test[2]=array();
$test[2]['hello']='hello';
print_r($test);

输出将是:

Array ( [0] => Array ( [hello] => hello ) [1] => Array ( [hello] => hello ) [2] =>
Array ( [hello] => hello ) )

这就是我想要输出的方式

所以这就是我在剧本中所做的事情

所以基本上在我的数据库中,我有一张女子联赛积分榜,而且这些专栏是

team_name, played, won, drawn, lost, for, against, points

所有连接都已成功处理,以下是我的查询

$get_ladies_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_ladies` order by pos";

在我展示下一个代码之前的重要一点是,还有另外两个榜单表,men_senior和men_intermediate具有相同的结构,但显然只有数据更改,下面是两个查询只是

$get_mens_inter_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos";

    $get_mens_senior_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos";

现在我创建3个阵列,我想分别为女士,男士高级,男士中级分别保持积分榜

$standings_ladies = array();
$standings_men_inter = array();
$standings_men_senior = array();

我希望在数组中显示的数据是这样的

array(0=>array(team_name,wins,drawn,lost,for,against,points)
1=>array(team_name,wins,drawn,lost,for,against,points)) and so on

既然我想为所有3个类别创建多维排名数组,我可以在3个单独的循环中运行查询,尽管我认为,我可以在1中完成相同的结果,我觉得它有助于提高性能。如果最好使用3 while循环,请告诉我,我尝试的是下面的内容。

//I run the 3 queries and store them in the given variables
$result_mens_senior = mysqli_query($link,$get_mens_senior_query);
$result_mens_inter = mysqli_query($link,$get_mens_inter_query);
$result_ladies= mysqli_query($link, $get_ladies_query);

//I want to create 1 while loop so based of the results returned from the 3 
//queries so based on the results returned from the 3 queries, 
//I get the max number of times I want the query to run
$ladies_boundary = mysqli_num_rows($result_ladies);
$mens_senior_boundary = mysqli_num_rows($result_mens_senior);
$mens_inter_boundary = mysqli_num_rows($result_mens_inter);
$max_size = max(array($ladies_boundary,$mens_senior_boundary,$mens_inter_boundary));

//set an index to start from 0
$index = 0;

//I will only show the example for 1 of the arrays but you get an idea
that this issue will occur for all
while ($index < $max_size)
{
//first, everytime the loop is entered, we need the next row to be fetched

    $standings_men_inter_table = mysqli_fetch_assoc($result_mens_inter);
    $standings_ladies_table = mysqli_fetch_assoc($result_ladies);

//there is a high chance that the other two tables return a different row size
//so its best to check that we do not go beyond
if($index < $mens_senior_boundary)
    {
            //we fetch the rows every time we enter the block
            $standings_men_senior_table = mysqli_fetch_assoc($result_mens_senior);

            //then, this is how I attempt at creating the 2 dimensional array
        array_push($standings_men_senior, array(
        $standings_men_senior_table['team_name'],
        $standings_men_senior_table['played'],
        $standings_men_senior_table['won'],
        $standings_men_senior_table['drawn'],
        $standings_men_senior_table['lost'],
        $standings_men_senior_table['for'],
        $standings_men_senior_table['against'],
        $standings_men_senior_table['points']));
    }

//incrementing index each time the loop runs
$index++;

}

然后最后,我只是想打印我认为是阵列但得到这个附加图像,希望你能清楚地看到它 enter image description here

为了进一步调查,每次输入“if”块时,我只是将所有内容评论出来,然后将其放入以查看返回的内容

if($index < $mens_senior_boundary)
{
    print_r(mysqli_fetch_assoc($result_mens_senior));
}

我得到的输出几乎是我需要的90%

Array 
([team_name] => Morley Gaels [played] => 8 [won] => 6 [drawn] => 2 
[lost] => 0 [for] => 110 [against] => 83 [points] => 14 ) 
Array ( [team_name] => Southern Districts [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 104 [against] => 98 [points] => 8 ) 
Array ( [team_name] => St Finbarrs [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 107 [against] => 99 [points] => 8 ) 
Array ( [team_name] => Western Shamrocks [played] => 8 [won] => 3 [drawn] => 0 
[lost] => 5 [for] => 96 [against] => 88 [points] => 6 ) 
Array ( [team_name] => Greenwood [played] => 8 [won] => 1 [drawn] => 1 
[lost] => 9 [for] => 82 [against] => 109 [points] => 3 )

我需要的是例如:

Array(0=>Array 
([team_name] => Morley Gaels [played] => 8 [won] => 6 [drawn] => 2 
[lost] => 0 [for] => 110 [against] => 83 [points] => 14 )
1=>Array
([team_name] => Southern Districts [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 104 [against] => 98 [points] => 8 )..... so on);

我的问题是

  • 我的代码有什么问题,动态创建的正确方法是什么 php中的多维数组?
    • 是否有一些我对mysql_fetch_assoc如何工作及其返回方式无法理解?
    • 有什么需要改进的,我做错了什么?

我感谢你的时间,而不是阅读它,我尽量详细了解我的尝试。

谢谢。

2 个答案:

答案 0 :(得分:1)

不要试图将所有内容放在一个循环中,它会显着降低代码清晰度,并且会给你带来很少甚至没有性能提升,有一个术语,它被称为微优化,你永远不应该做它除非确实有必要(大型网站上的瓶颈)。

现在,我建议您重新制作循环,尽可能清楚地了解数据的操作方式。

在您的情况下,最重要的是在流程的每一步调试,print_r数组的内容,以检查它包含的内容,从数据库到代码的末尾,您将找到问题所在。

答案 1 :(得分:1)

试试这个

执行此操作后:

$result_mens_senior = mysqli_query($link,$get_mens_senior_query);
$result_mens_inter = mysqli_query($link,$get_mens_inter_query);
$result_ladies= mysqli_query($link, $get_ladies_query);

就这样做

while ($standings_men_senior[] = mysqli_fetch_assoc($result_mens_senior)){}
while ($standings_men_inter[] = mysqli_fetch_assoc($result_mens_inter)){}
while ($standings_ladies[] = mysqli_fetch_assoc($result_ladies)){}

基本上所有发布的代码都应该替换为:

<?php
$ladies_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_ladies` order by pos";

$inter_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos";

$senior_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos";

$ladies_stmt = mysqli_query($link, $ladies_query) || die ("Couldn't get Ladies"); // reminds me of high school
$inter_stmt  = mysqli_query($link, $inter_query)  || die ("Couldn't get Inter");
$senior_stmt = mysqli_query($link, $serior_query) || die ("Couldn't get Seniors");

$standings_men_senior = array();
$standings_men_inter = array();
$standings_ladies = array();

while ($row = mysqli_fetch_assoc($senior_stmt)){
    $standings_men_senior[] = $row;
}
while ($row = mysqli_fetch_assoc($inter_stmt)){
    $standings_men_inter[] = $row;
}
while ($row = mysqli_fetch_assoc($ladies_stmt)){
    $standings_ladies[] = $row;
}