开发者

How do I name an array key with a key inside the array

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=arra开发者_C百科y();  
    $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.


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.


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.


$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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜