Forum Moderators: coopster
This is my first post here and I am hoping someone nice will know the answer to this right off the top of their head.
I have and array
$array = array();
and I have a function
function get_groups(){
global $db;
$group_array = array();
$groups = $db->Execute("select group_id, group_description
from " . TABLE_GROUPS . "
order by group_description");
while (!$groups->EOF) {
$group_array[] = array('id' => $groups->fields['group_id'],
'text' => $groups->fields'group_description']);
$groups->MoveNext();
}
return $group_array;
}
when I set my array to my function
$array = array(get_groups())
my sizeof($customer_group) = 1
I know my assignment must be wrong. Is this possible and if so, how?
$group_array[]=
Here's your problem. You need to do an array_append(), or increment the array index. This way, you are always replacing the entire array (I think -I never use this syntax, so maybe there is a trick, where the index is always incrementing?)
Try this instead:
array_append($group_array,array('id' => $groups->fields['group_id'],
'text' => $groups->fields'group_description']));
function get_array() { return ['a', 'b', 'c']; } $var = [get_array()]; [/code]
[code]sizeof($var);1That's odd. Why is there only one element in $var? To understand, let's see what $var contains:
[/code][code]print_r($var);[['a', 'b', 'c']]Ah, enlightenment! $var is an array of one element, the array returned by get_array(). Perhaps if we don't wrap an array() call around get_array() we'll get what we want?
[/code][code]$var = get_array(); sizeof($var);3print_r($var);['a', 'b', 'c']Why, yes, we do. It turns out we don't have to know the function returns an array to get an array from the function any more than we have to know the function returns a string or an integer to get a string or an integer back.