i'm getting a array of user names doing a sql query.it's like this
Array (
[0] => Array (
[users] => Array (
[displayname] => Mark
)
)
[1] => Array (
[users] => Array (
[displayname] => Helan
)
)
[2] => Array (
[users] => Array (
[displayname] => Shaun
)
)
[3] => Array (
[users] => Array (
[displayname] => Basu
)
)
[4] => Array (
[users] => Array (
[displayname] => Charit
开发者_如何学Python )
)
[5] => Array (
[users] => Array (
[displayname] => Chris
)
)
[6] => Array (
[users] => Array (
[displayname] => Tony
)
)
[7] => Array (
[users] => Array (
[displayname] => Sam
)
)
[8] => Array (
[users] => Array (
[displayname] => Duck
)
)
[9] => Array (
[users] => Array (
[displayname] => Frank
)
)
)
i want to recreate the array to this format
Array ( 'Mark' => 'Mark','Helan' => 'Helan' , ..........,'Frank'=>'Frank')
How can i do this?
$users = $this->User->find('list',array('fields'=>array('displayname','displayname')));
if you use the find('list') method, you could specify the fields and the result would be a simple key/value array.
Another way to do it is using the Set Object from the Core utility library. See the extract() method
Hope this helps
It seems that Set::extract() is what you need here, with array_combine(). Here is an example:
// $data contains your find result
$names = Set::extract('/users/displayname', $data);
$formattedData = array_combine($names, $names)
What you're actually asking for is Set::combine which is basically a combination of what Pierre MARTIN suggests.
$names = Set::combine($data, '{n}.users.displayname', '{n}.users.displayname');
精彩评论