Maff
Maff

Reputation: 439

parse data within multidimensional array using php

I'm finding this hard to explain to know how to search for an answer for this. With the help of you lovely people yesterday I cut down ~1200 MySQl queries to just 2, but now I'm having trouble doing anything with the results. This is the array I'm getting back (snippet)

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

The final output I'm working towards is the total number, per person, per priority, per withinfix, so for example, the last 2 lines could give a result like:

$AdrianHullP3Fix1 = 771;

$AdrianHullP4Fix1 = 4;

But I have no idea how to go about this from the results I have, can anyone recommend a solution or just the correct php method/function for me to investigate?

If it helps, this is the code I'm using to create the array from the query:

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

Upvotes: 1

Views: 36

Answers (3)

Jeetendra Chauhan
Jeetendra Chauhan

Reputation: 1977

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

${} will create new dynamic variables

Hope this help

Upvotes: 1

Alankar More
Alankar More

Reputation: 1115

Hope this will help you.

// 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 

Upvotes: 1

Maff
Maff

Reputation: 439

May not be graceful, but I've figured it out using implodes and explodes

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
}

Upvotes: 0

Related Questions