0

i'm probably make a really stupid mistake, but i can't find it.

i'm trying to return an array, and it just isn't working.

the count of $temp_array is 33 which is the correct number it should be, the count of $dropdown_array is 1. what am i doing wrong?

<?php
function dropmaker($stuff) {

require '../connect.php';

$dropdown_query = "SELECT $stuff FROM maps_sku_groups GROUP BY $stuff;";
$dropdown_result = mysqli_query($link, $dropdown_query);
while ($data = mysqli_fetch_array($dropdown_result)) {
    $temp_array[] = $data[$stuff];
}
echo count($temp_array);
return array($temp_array);
}?>

<?php 
$dropdown_array[] = dropmaker('cyclecount');
echo count($dropdown_array);
?>
3
  • I wouldn't include a file in a function! or at least use: require_once Commented Dec 2, 2014 at 16:28
  • did you try using var_dump($arrayname) or print_r($arrayname) to see the results? Also, as @Rizier123 mentioned, don't include a connection inside a function. Require it once at the top of your script, this can cause bugs. Commented Dec 2, 2014 at 16:29
  • i can't seem to get the function to work at all without having the file in it. which also makes no sense to me... Commented Dec 2, 2014 at 16:31

4 Answers 4

6

You are returning an array, which contains the array $temp_array as its only element. Just return $temp_array directly, and it should work.

EDIT (Thanks @Rizier123): Additionally, you are assigning the result of the call to dropmaker() to $dropdown_array[], which inserts the result of the function call into the (previously non-existing) array $dropdown_array, so $dropdown_array becomes an array with the function result as its only element.

Sign up to request clarification or add additional context in comments.

2 Comments

Also he don't have to write this: $dropdown_array[] only $dropdown_array
e. g a simple var_dump($dropdown_array) would've shown this problem.
1

The problem is this line $dropdown_array[] = dropmaker('cyclecount'); you're assigning the return value of dropmaker(), an array, to the first element of the array $dropdown_array[] rather than assigning $dropdown_array to the array itself.

Try this code

$dropdown_array = dropmaker('cyclecount');

Removing the [] is fixing this issue.

Comments

1

Try This Code :

return array($temp_array) instead of return $temp_array

Also $dropdown_array[] instead of $dropdown_array

Comments

1

Try this

<?php
    function dropmaker($stuff) {

    require '../connect.php';

    $dropdown_query = "SELECT $stuff FROM maps_sku_groups GROUP BY $stuff;";
    $dropdown_result = mysqli_query($link, $dropdown_query);
    while ($data = mysqli_fetch_array($dropdown_result)) {
        $temp_array[] = $data[$stuff];
    }
    echo count($temp_array);
    return $temp_array;
    }?>

    <?php 
    $dropdown_array = dropmaker('cyclecount');
    echo count($dropdown_array);
    ?>

Your function returns array of array since you have written return array($temp_array);

And also you are assigning this return value to first index of array $dropdown_array so the count of this variable remains 1

1 Comment

when doing this it still is 33 & 1 the var_dump gives this: (which is one level less than my previous iteration.) array (size=1) 0 => array (size=33)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.