简体   繁体   中英

Change array index of mysql query result set

This is mymysql table

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

when I am using select query, i will get result as:

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

I need to get this result as

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

using sql query

Here the index 5466 and 7856 are the field 'ssn' (this is a unique no to that person)

Do you want like this?

SQL column name ssn , result array index sin . I wrote as sin

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

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

this link same is being discussed

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

尝试使用mySQL索引提示语法索引提示语法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM