1

I'm trying to add some settings to my WordPress options page that depend on the number of categories. I created this function to use inside the main array, but it only returns the first array, leaving out the other 3 I have. A print_r will show all of them, so I can't seem to figure this out.

function listSections() {
  $categories = get_categories();
    foreach($categories as $category) {
      return array (
        "name" => $category->cat_name . " Label Color",
        "desc" => "Select a label color.",
        "id" => $shortname."_label_color" . $category->cat_ID,
        "type" => "select",
        "options" => $color_options,
        "std" => ""
      );
    }
}
2
  • 1
    You are returning inside a loop. That will cause the behaviour you describe. You need to add each hit to a temporary array, and return that after the loop Commented Aug 27, 2011 at 16:09
  • 1
    Think a little more about what return does. Commented Aug 27, 2011 at 16:09

4 Answers 4

4

You can only return once!

function listSections() {
  $categories = get_categories();
  $return = array();
    foreach($categories as $category) {
      $return[] = array (
        "name" => $category->cat_name . " Label Color",
        "desc" => "Select a label color.",
        "id" => $shortname."_label_color" . $category->cat_ID,
        "type" => "select",
        "options" => $color_options,
        "std" => ""
      );
    }
    return $return;
}

The fix is push each array into a temporary array, then return that array at the end of the loop.

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

5 Comments

@Fraser It wasn't when I started typing.
In order to work, each array needs to be added to the main array individually, so returning array(array()) will not work.
@Norbert Use your head. There's an infinite number of solutions to that problem e.g. passing in the main array by reference and pushing new arrays to that: listSections(&$mainarray) { ... $mainarray[] = array(...
@Fraser Since when was that an acceptable reason to down vote the answer anyway? You should only be down voting if it does not answer the question or is inaccurate.
sorry, didn't mean to be off - I apologies...I will reverse the vote asap. I got out of bed on the wrong side or something.
1

The function can only return once. It cannot return multiple things in a loop. After it reaches the first return, it exits the function completely. If you want to return an array of arrays, you should use the following.

function listSections() {
    $results = array();
    $categories = get_categories();

    foreach($categories as $category) {
        $results[] = array (
        "name" => $category->cat_name . " Label Color",
        "desc" => "Select a label color.",
        "id" => $shortname."_label_color" . $category->cat_ID,
        "type" => "select",
        "options" => $color_options,
        "std" => ""
        );
    }

    return $results;
}

using the syntax $result[] = xyz; will append xyz to the end of the array. You can loop through the returned array, with some code like

$results = listSections();

$count = count($results);
for ($i = 0; $i < $count; $i++) {
    $category = $results[$i];
    echo $category["name"];
    etc......
}

2 Comments

Is there a way to return each array separately (not inside another array) without using another loop outside the function?
The function can only return 1 value. To return multiple values, you have to return an array containing those values. Even if it could return multiple values, how would the calling program access those?
0

When you call return from a function it always immediately ends the execution of that function, so as soon as the first array gets returned, the function ends - which is why you are only getting the first array back.

You could try returning a multi-dimensional array (an array that contains all of the arrays you'd like to be returned) instead.

Comments

0

The goal of the returnkeyword is to exit the function. So it is normal that your fonction only return the first element. You can for exemple put all the elements into an array and return this array :

function listSections() {
  $categories = get_categories();
  $arr = array();
    foreach($categories as $category) {
      $arr[] =  array (
        "name" => $category->cat_name . " Label Color",
        "desc" => "Select a label color.",
        "id" => $shortname."_label_color" . $category->cat_ID,
        "type" => "select",
        "options" => $color_options,
        "std" => ""
      );
    }

    return $arr;
}

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.