tags:

views:

74

answers:

3

I have some data, yes, data. This data came from a MySQL query and it will always contain 4 items, always. I want to cache that data in an array table for use later within a web page but I want to keep the keys from the query and separate out each grouping within a multidimensional array. However to save time iterating through the array each time I want to find a given group of data, I want to call the keys of the first array the same as the ID key which is always the first key within each four items.

At the minute I'm using this code:

function mysql_fetch_full_result_array($result)  
{  
$table_result=array();  
$r=0;  
while($row = mysql_fetch_assoc($result)){  
    $arr_row=array();  
    $c=0;  
    while ($c < mysql_num_fields($result)) {        
        $col = mysql_fetch_field($result, $c);   
        $arr_row[$col -> name] = $row[$col -> name];           
        $c++;  
    }     
    $table_result[$r] = $arr_row; 
    $r++;  
}     
return $table_result;  
}  

I'm currently testing this using 3 unique users, so I'm getting three rows back from the query and the data from this function ends up in the format:

 [0]=>  
  . .   [id]   => 1    
  . .   [name] => random name     
  . .   [tel]  => random tel  
  . .   [post] => post code data  
  [1]=>  
  . .   [id]   => 34   
  . .   [name] => random name     
  . .   [tel]  => random tel  
  . .   [post] => post code data  
  [2]=>   
  . .   [id]   => 56   
  . .   [name] => random name      
  . .   [tel]  => random tel  
  . .   [post] => post code data  

So how do I alter the code to instead of the keys [0], [1], [2] give me the output:

  [1]=>  
  . .   [id]   => 1    
  . .   [name] => random name     
  . .   [tel]  => random tel  
  . .   [post] => post code data  
  [34]=>  
  . .   [id]   => 34   
  . .   [name] => random name     
  . .   [tel]  => random tel  
  . .   [post] => post code data  
  [56]=>   
  . .   [id]   => 56   
  . .   [name] => random name      
  . .   [tel]  => random tel  
  . .   [post] => post code data 

I don't mind if the main array keys are strings of numbers rather than numbers but I'm a bit stuck, I tried changing the $table_result[$r] = $arr_row; part to read $table_result[$result['id']] = $arr_row; but that just outputs an array of one person. I know I need another loop but I'm struggling to work out how to write it.

A: 
$arr_row[(name of ID field in your result)][$col -> name] = $row[$col -> name]

That should set your array key as the ID, so it should assign, for example:

$arr_row[34]['id'] = 34;
$arr_row[34]['name'] = 'name';
$arr_row[34]['tel'] = 'tel';
$arr_row[34]['post'] = 'post';
Kevin
A: 

Change the line:

$table_result[$r] = $arr_row; $r++;
to
$table_result[$arr_row['id']] = $arr_row;

And I don't think you need the $r any more.

easement
That worked wonderfully, cheers!
Confused
A: 

How about:

$table_result = array();
$sql = "SELECT id, name, tel, post FROM sometable";
$res = mysql_query($sql);
if (mysql_error()) {
    die("MySQL error: " . mysql_error());
}

while($row = mysql_fetch_array($res)) {
    $table_result[$row['id']] = $row;
}

If, as you say, you don't mind the ID field being repeated within each array element, then there's no need to loop over the individual rows and extract fields.

Marc B
And also, having just seen what this outputs many thanks to you too! I might need to rewrite some sections to take advantage of that far simpler code, than using the entire function call.
Confused
Seriously that solution is genius, I hadn't realised that just adding an array like that would work, I'd been staring at the code for most of the night and coming up with ever more elaborate solutions. Having just adapted my code to use that, it's far simpler and saves all sorts of faffing around. If I could rate you up I would!
Confused