Page 1 of 1

Convert array(array()) to array()

Posted: Sun Apr 11, 2010 4:03 am
by Weiry
Ok it may or may not be as simple as the topic says.
I am trying to impliment a jQuery autocomplete script which retrieves usernames from the database upon typing in a few letters.
jQuery Autocomplete Plugin

Currently I am receiving an array of data in this format from my database:

Code: Select all

Array
(
    [0] => Array
        (
            [id] => 1
            [user_id] => 63
            [name] => Weiry
        )
)
However i need to convert this into the following format:

Code: Select all

Array
(
    [Weiry] => 63
)
Problem is im not entirely sure how to set key=>value's for the parent array when sorting through the current array dataset.
This is the current code i have which sort of comes close.. but not quite.

Code: Select all

foreach($items as $char){
	$testArr[] = array("{$char['name']}" =>"{$char['user_id']}");
}
You will notice that this code would lead to an output of

Code: Select all

Array
(
   [0] => Array
      (
         [Weiry] => 63
      )
)
I have tried changing the the 2nd line of the code to:

Code: Select all

$testArr[] = "{$char['name']}" => "{$char['user_id']}";
Without the array(), but then it just dies hard.

This is the function which returns the data as an array. Keep in mind that im not sure how much more i could change it, because i NEED the default array to be returned. (Yes the function is private, but im accessing it through a public method for security :P )

Code: Select all

private function getChar($id=false,$name=false){
		$q = "SELECT * FROM ".TBL_CHARS;
		if($name){	$q .= " WHERE `name` = '{$name}'";
		}elseif($id){	$q .= " WHERE `id` = '{$id}'";}
		
		$r = $this->database->query($q);
		if($this->database->numRows($r) > 0){
			if(!$id && !$name){
				while($row = $this->database->fetchAssoc($r)){
					$charArr[] = $row;
				}
				return $charArr;
			}
			return $this->database->fetchAssoc($r);
		}
		return false;
	}
Suggestions :D?

Re: Convert array(array()) to array()

Posted: Sun Apr 11, 2010 5:13 am
by requinix
I suggest using id=>name instead of name=>id.

When you use [] to add stuff to an array you don't have to leave it empty. You can put a key in there.

Code: Select all

foreach($items as $char){
        $testArr[$char["user_id"]] = $char["name"];
}
And please,

Code: Select all

"{$char['name']}"
putting variables in quotes like that, without anything else in the string, accomplishes absolutely no good.

Re: Convert array(array()) to array()

Posted: Sun Apr 11, 2010 5:55 am
by Weiry
tasairis wrote:I suggest using id=>name instead of name=>id.
While you suggest that, that is not what i need to accomplish. Your suggestion actually breaks the code provided as a part of the jQuery plugin and i would rather not rewrite existing code.
tasairis wrote: When you use [] to add stuff to an array you don't have to leave it empty. You can put a key in there.

Code: Select all

foreach($items as $char){
        $testArr[$char["user_id"]] = $char["name"];
}
Thats exactly what i was after, thanks :D.
tasairis wrote: putting variables in quotes like that, without anything else in the string, accomplishes absolutely no good.
Force of habit keeping everything the same wherever there are strings. I got rid of that formatting though.