更改mysql查询结果集的数组索引

时间:2013-07-31 06:52:00

标签: php mysql sql codeigniter

这是mymysql表

id  name   ssn    phone       email**  
1   Asok   5466   9865893265  asok@gmail.com  
2   Sokan  7856   9562358965  sakan@gmail.com
......  
.....

当我使用select查询时,我会得到结果:

Array ( [0] => Array ( [id] => 1 [name] => Asok [sin] => 5466 [phone] => 9865893265 [email] => asok@gmail.com )  [1] => Array ( [id] => 2 [name] => Sokan [sin] => 7856 [phone] => 9562358965 [email] => sakan@gmail.com ) ...)`

我需要将此结果作为

Array ( [5466] => Array ( [id] => 1 [name] => Asok [sin] => 5466 [phone] => 9865893265 [email] => asok@gmail.com )  
[7856] => Array ( [id] => 2 [name] => Sokan [sin] => 7856 [phone] => 9562358965 [email] => sakan@gmail.com ) ...)

使用sql查询

这里的索引5466和7856是字段'ssn'(对于那个人来说这是一个独特的否)

3 个答案:

答案 0 :(得分:1)

你想这样吗?

SQL列名ssn,结果数组索引sin。我写作sin

$newArray = array();
foreach ($results as $row)
{
   $newArray[$row['sin']] = $row;
}

答案 1 :(得分:0)

https://github.com/EllisLab/CodeIgniter/pull/429

此链接正在讨论

functions map_array($key_field, $value_field) and map($key_field, $value_field)
that return a map (dictionary) from the result set

答案 2 :(得分:0)

尝试mySQL索引提示语法 index hint syntax