# Foreach loop with more than 50 results hangs up the system

**URL:** <https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 13, 2017, 3:20am UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252 "2017-12-13T03:20:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 3:20am UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/1 "2017-12-13T03:20:10Z")

</div>

I’m using

> **[GitHub - rasteiner/controlledlist: A Kirby 2 CMS plugin to extend the functionality...](https://github.com/rasteiner/controlledlist)**
>
> A Kirby 2 CMS plugin to extend the functionality of list options

But when I try and pass a foreach array through this with more than 50 results. It hangs up .

I can use this.

```auto
      $start = 1;
        $end = 1123;
        $values = array();

        for ($start; $start < $end; $start = $start + 1) {
            $values[$start] = $start."helloworld";
        }
return($values);

```

It works just fine. But when I try something like.

```auto
   $values = array();
       foreach ($users as $user) {
          $test[$user->name] =
          $user->name;
           $values = array_slice($test, 0,45);
      }
 // print_r($output);
return $values;

```

I sliced the array to 45 and it hangs, if I put it down to 5 it works just fine, but still slow.

It freezes up. Any idea why this might be happening?

It uses this to pass the results in.

```auto
class ControlledradioField extends RadioField {
	public function options() {
		return call_user_func($this->controller, $this);
	}
}

```

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 6:11am UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/3 "2017-12-13T06:11:51Z")

</div>

It must be something in my array because I run the following code and it works just fine.

```auto
 static function userlist($field) {

        $start = 1;
        $end = 1123;
        $values = array();

        for ($start; $start < $end; $start = $start + 1) {
            $values[$start] = "helloworld";

        }

        echo "somethingelse";
       // print_r($values);
        $theseVals = $values;

        // $kirby = kirby();
        //$site = $kirby->site();
       // $users = $site->users();
// print_r($theseVals);
        $result = array();

        foreach ($theseVals as $key => $value) {
           $result[$key] = $value;
        }

       return $result;
    }
}

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 7:06am UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/4 "2017-12-13T07:06:53Z")

</div>

I’m a bit suprised that the above works at all, because the User class does not have a `name` method:

```auto
class Test {
    static function userlist($field) {
        $kirby = kirby();
        $site = $kirby->site();
        $users = $site->users();

        $result = array();

        foreach ($users as $user) {
            $result[$user->username()] =
                $user->username();
        }
        return $result;
    }
}

```

Can’t test with that many users, though.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 6:44pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/5 "2017-12-13T18:44:44Z")

</div>

So, I now script-generated 100 users and tested with those. And I can confirm that it takes very long to generate the options list when using checkboxes, although the system does not hang itself up. The result is way faster with select or radio fields. While I doubt that such a long list of options really makes sense, this is still weird.

I recall there was an issue on GitHub regarding checkboxes and JSON API queries, but that was fixed in June. And when using the JSON API now, the options are created fast, at least after the second load.

I also tested the above user example without a named key and it way a lot faster.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 6:54pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/6 "2017-12-13T18:54:38Z")

</div>

If we are referencing back to the other post. [Checkboxes creating a funny loop with input values (Possible Bug) or Improvement](https://forum.getkirby.com/t/checkboxes-creating-a-funny-loop-with-input-values-possible-bug-or-improvement/9260)

It’s somehow returning the number of array $keys, plus 1 array.

this array.

$array = array(‘green’ =\> ‘blue’, ‘tan’=\> ‘red’, ‘tan’ =\> ‘green’,);

Gives this result. Also notice it combines any simular $keys but also counts the array then loops through that many times. + 1.

Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green ) Array ( [green] =\> blue [tan] =\> green )

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 7:02pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/7 "2017-12-13T19:02:06Z")

</div>

> [@bbarclay](#):
>
> $array = array(‘green’ =\> ‘blue’, ‘tan’=\> ‘red’, ‘tan’ =\> ‘green’,);

This array does not make sense. You can’t have duplicate keys.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 7:04pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/8 "2017-12-13T19:04:22Z")

</div>

It was to verify that it’s counting. But yeah, we don’t have duplicate keys. I just wanted to see if it ignores it or counts it, which I’m pretty sure it’s counting it.

Update. Or it’s not

$array = array(‘green’ =\> ‘blue’, ‘tan’=\> ‘red’, ‘pink’ =\> ‘green’,); This returns the array below. Which is 10 so it’s 3 times the loop plus one.

Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green ) Array ( [green] =\> blue [tan] =\> red [pink] =\> green )

Just tested

$array = array(‘green’ =\> ‘blue’);

Array ( [green] =\> blue ) Array ( [green] =\> blue ) Array ( [green] =\> blue ) Array ( [green] =\> blue )

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 7:08pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/9 "2017-12-13T19:08:17Z")

</div>

I think I’ve lost track. What are you doing with the array to get the above result?

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 7:13pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/10 "2017-12-13T19:13:07Z")

</div>

I thought you were refering to this. [Checkboxes creating a funny loop with input values (Possible Bug) or Improvement](https://forum.getkirby.com/t/checkboxes-creating-a-funny-loop-with-input-values-possible-bug-or-improvement/9260)

Which from here. I use the plugin.

Then run the plugin

```auto
class MyPlugin {

   static function getpermissionslist() {
       $array = array('green' => 'blue');
       print_r($array);
}

}

```

in yaml I do like on the other page and also use the plugin on the other page. If I change this to radio button the array only returns once.

It’s like something is hitting this multiple times causing it to loop the array and that’s what’s slowing down the return time.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 7:19pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/11 "2017-12-13T19:19:49Z")

</div>

Ah, ok, I don’t get multiples, though.

This:

```auto
class Test {
  static function userlist() {
      $array = array('green' => 'blue', 'yellow' => 'black');
      dump($array);
  }
}

```

dumps

```auto
Array
(
    [green] => blue
    [yellow] => black
)

```

as it should.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 7:21pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/12 "2017-12-13T19:21:16Z")

</div>

Crazy. Could this be a plugin interfeiring?  
Array  
(  
[green] =\> blue  
)  
Array  
(  
[green] =\> blue  
)  
Array  
(  
[green] =\> blue  
)  
Array  
(  
[green] =\> blue  
)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 7:21pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/13 "2017-12-13T19:21:49Z")

</div>

That’s what I was just going to ask, have you tested this with a nice, fresh 🥗 Starterkit yet? I haven’t tested with 2.5.6 yet, but I don’t think there have been any changes from 2.5.6 to 2.5.7 that could be causing this behavior.

Edit: As expected, 2.5.6 gives me the same results.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 8:00pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/14 "2017-12-13T20:00:41Z")

</div>

My fault was using.

```auto
    static function getpermissionslist() {
        $array = array('green' => 'blue');
        dump($array);
return $array;

```

But still large queries take forever with the checkboxes. We have 500 options and are using.

> **[distantnative/field-multiselect](https://github.com/distantnative/field-multiselect)**
>
> field-multiselect - Multiselect field for Kirby 2 CMS

  
so checkes are organized, but the regular checkbox is slow to filter through the results.

Hope this helps and there is a solution to parse them in faster.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 8:15pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/15 "2017-12-13T20:15:31Z")

</div>

Isn’t 500 options a bit overdoing it with regular checkboxes? I still have a feeling that it is faster without named keys.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 8:18pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/16 "2017-12-13T20:18:18Z")

</div>

They are internal company permissions. The permissions have ID’s but dev is saying the permissions are all stored by name, because that is the practice for those permissions. So if I ever reference them, they have to be by name. I wish I could and there weren’t that many. Not my decision though. lol.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 8:24pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/17 "2017-12-13T20:24:48Z")

</div>

How about using the JSON API instead of the controlledList plugin?

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [December 13, 2017, 8:26pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/18 "2017-12-13T20:26:52Z")

</div>

These are all coming out of a JSON webservice. It’s not the webservice that’s slow though, I’ve eliminated that.

Can the JSON API be used with webservices calls?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2017, 8:28pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/19 "2017-12-13T20:28:04Z")

</div>

Via a route that shouldn’t be a problem at all.

If your web service call is a URL that returns nothing else than JSON encoded key/value pairs, it might even work directly (have never tested that)

Field:

```auto
  mycheckboxes:
    label: Users
    type: checkboxes
    options: url
    url: api/users.json

```

route:

```auto
c::set('routes', array(
  array(
    'pattern' => 'api/users.json',
    'action' => function () {

     // your web service call instead of this, of course
      $users = site()->users();

      $result = array();

      foreach ($users->limit(60) as $user) {
          $result[$user->username()] = $user->username();
      }
      return response::json($result);
    }

  )
));

```

It’s at least worth a test. Maybe you can even cache the results?

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:30pm UTC](https://forum.getkirby.com/t/foreach-loop-with-more-than-50-results-hangs-up-the-system/9252/20 "2025-02-06T17:30:18Z")

</div>


