1

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
        )
    )
    [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?

4 Answers 4

2
$users = $this->User->find('list',array('fields'=>array('displayname','displayname')));
Sign up to request clarification or add additional context in comments.

1 Comment

I think that that won't give you the displayname as the key
2

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

Comments

1

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)

Comments

1

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.