使用php解析多维数组中的数据

时间:2015-03-11 08:40:02

标签: php arrays multidimensional-array

我发现很难解释如何寻找答案。在你们可爱的帮助下,昨天我将约1200个MySQl查询减少到2个,但现在我无法对结果做任何事情。这是我要回来的数组(片段)

Array 
( 
[0] => Array ( [total] => 7 [closedby] => Adam_Howard [priority] => P3 [withinfix] => 0 ) 
[1] => Array ( [total] => 20 [closedby] => Adam_Howard [priority] => P3 [withinfix] => 1 ) 
[2] => Array ( [total] => 3 [closedby] => Adam_Howard [priority] => P4 [withinfix] => 0 ) 
[3] => Array ( [total] => 5 [closedby] => Adam_Howard [priority] => P4 [withinfix] => 1 ) 
[4] => Array ( [total] => 3 [closedby] => Adam_Jones [priority] => P3 [withinfix] => 0 ) 
[5] => Array ( [total] => 2 [closedby] => Adam_Jones [priority] => P3 [withinfix] => 1 ) 
[6] => Array ( [total] => 11 [closedby] => Adrian_Dimmock [priority] => P2 [withinfix] => 0 ) 
[7] => Array ( [total] => 39 [closedby] => Adrian_Dimmock [priority] => P2 [withinfix] => 1 ) 
[8] => Array ( [total] => 20 [closedby] => Adrian_Dimmock [priority] => P3 [withinfix] => 0 ) 
[9] => Array ( [total] => 301 [closedby] => Adrian_Dimmock [priority] => P3 [withinfix] => 1 ) 
[10] => Array ( [total] => 2 [closedby] => Adrian_Dimmock [priority] => P4 [withinfix] => 0 ) 
[11] => Array ( [total] => 33 [closedby] => Adrian_Dimmock [priority] => P4 [withinfix] => 1 ) 
[12] => Array ( [total] => 37 [closedby] => Adrian_Hull [priority] => P2 [withinfix] => 0 ) 
[13] => Array ( [total] => 1211 [closedby] => Adrian_Hull [priority] => P2 [withinfix] => 1 ) 
[14] => Array ( [total] => 4 [closedby] => Adrian_Hull [priority] => P3 [withinfix] => 0 ) 
[15] => Array ( [total] => 771 [closedby] => Adrian_Hull [priority] => P3 [withinfix] => 1 ) 
[16] => Array ( [total] => 4 [closedby] => Adrian_Hull [priority] => P4 [withinfix] => 1 )
)

我正在努力的最终输出是每个人,每个优先级,每个内部的总数,所以例如,最后两行可以给出如下结果:

$AdrianHullP3Fix1 = 771;

$AdrianHullP4Fix1 = 4;

但我不知道如何从我的结果中解决这个问题,有人可以推荐一个解决方案或只是正确的php方法/功能供我调查吗?

如果有帮助,这是我用来从查询创建数组的代码:

while($row = mysql_fetch_assoc($Query)){
$results[] = $row;
}

3 个答案:

答案 0 :(得分:1)

while($row = mysql_fetch_assoc($Query)){
    ${$row['closedby'].$row['closedbypriority'].$row['closedbywithinfix']}  = $row['total'];
}

$ {}将创建新的动态变量

希望这个帮助

答案 1 :(得分:1)

希望这会对你有所帮助。

// this will be the word that you want to attached to a variable
define('WORD_TO_ADD','Fix');

while($row = mysql_fetch_assoc($Query)){
$variableName = str_replace("_","",$row['closedby']).$row['priority'].WORD_TO_ADD.$row['withinfix'];
    $$variableName = $row['total'];
}
echo $AdrianHullP3Fix1; // o/p 771 
echo $AdrianHullP4Fix1; // o/p 4 

答案 2 :(得分:0)

可能不是很优雅,但我已经发现使用内爆和爆炸

while($row = mysql_fetch_assoc($Query)){
$rowString = implode ("_", $row); //make the row into a string
$minusTotalParts = explode("_", $rowString); // explode it by the underscores
$minusTotal = $minusTotalParts[0]."_".$minusTotalParts[1].$minusTotalParts[2]."Fix".$minusTotalParts[3]; //create the variable string
eval('return $'.$minusTotal.' = '.$minusTotalParts[4].';'); //create the variable of $firstname_surnameP2Fix0 where the SLA and withinfix changes each time, it then give it the total value
}