函数仅返回第一个数组

发布于 2024-12-01 20:37:01 字数 565 浏览 0 评论 0原文

我正在尝试向 WordPress 选项页面添加一些取决于类别数量的设置。我创建了这个函数在主数组中使用,但它只返回第一个数组,忽略了我拥有的其他 3 个数组。 print_r 将显示所有这些,所以我似乎无法弄清楚这一点。

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" => ""
      );
    }
}

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" => ""
      );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

五里雾 2024-12-08 20:37:01

您只能返回一次!

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;
}

修复方法是将每个数组推入临时数组,然后在循环结束时返回该数组。

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.

聽兲甴掵 2024-12-08 20:37:01

该函数只能返回一次。它不能在循环中返回多个内容。到达第一次返回后,它完全退出该函数。如果你想返回一个数组的数组,你应该使用以下内容。

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;
}

使用语法 $result[] = xyz;将把 xyz 添加到数组的末尾。您可以使用类似的代码循环返回的数组

$results = listSections();

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

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......
}
萌吟 2024-12-08 20:37:01

当您从函数中调用 return 时,它总是立即结束该函数的执行,因此一旦返回第一个数组,函数就会结束 - 这就是为什么您只返回第一个数组的原因。

您可以尝试返回一个多维数组(一个包含您想要返回的所有数组的数组)。

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.

琉璃梦幻 2024-12-08 20:37:01

return关键字的目标是退出函数。所以你的函数只返回第一个元素是正常的。
例如,您可以将所有元素放入一个数组中并返回该数组:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文