CodeIgniter Forums
Possible to return database row with a key instead as numeric indexes? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Possible to return database row with a key instead as numeric indexes? (/showthread.php?tid=61166)



Possible to return database row with a key instead as numeric indexes? - El Forum - 10-04-2014

[eluser]Unknown[/eluser]
For example if I do

Code:
Select * from Persons

Where Person has the columns: Name (Primary Key), Address, Phone

And I turn the returned Rows as an array I'll get

Code:
[0] => Array
        (
            [Name] => John Mang,
            [Address] => 123 Fake St,
            [Phone] => 1234567
        )

[1] => Array
        (
            [Name] => George Mang,
            [Address] => 123 Not Fake St,
            [Phone] => 555555
        )

But is there a way to get it so it'll be like

Code:
["John Mang"] => Array
        (
            [Name] => John Mang,
            [Address] => 123 Fake St,
            [Phone] => 1234567
        )

["George Mang"] => Array
        (
            [Name] => George Mang,
            [Address] => 123 Not Fake St,
            [Phone] => 555555
        )

Thanks.


Possible to return database row with a key instead as numeric indexes? - El Forum - 10-04-2014

[eluser]rufnex[/eluser]
You have to reorganize your result array like that for e.g.

Code:
$result = $query->result_array();
            $hash = array();
            foreach ($result as $array)
            {
                $hash[$array['Name']] = $array;
            }
            print_r($hash);