Forum Moderators: coopster

Message Too Old, No Replies

set an array to a function return

         

prosimma

11:57 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



Hello there.

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?

cmarshall

12:05 am on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Welcome to WebmasterWorld!

2) I'm not sure I understand the question. You cannot assign a value to count(). That is a function.

3) There should be no problem returning an array as a function return. You don't need to preallocate an array in either of the instances you show.

prosimma

12:10 am on Apr 6, 2007 (gmt 0)

10+ Year Member



Thanks!

By "my sizeof($customer_group) = 1", I meant that my count is only one when my function is returned.

I know that my function is looping 3 times. So, I am expecting $array to have a a count of 3.

I thought this would be possible. I will keep at it then.

cmarshall

12:40 am on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

pinterface

7:40 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Okay, let's simplify that a bit to remove extraneous details ([] is my shorthand for array()):

    function get_array() { return ['a', 'b', 'c']; }

    $var = [get_array()];

    [/code]
    [code]sizeof($var);
      =>
      1

That'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);
      =>
      3

    print_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.