Kirby methods on arrays

Hey there.
I’m getting an array via API calls and want to use it with Kirby’s internal functions, like flip, filterBy and so on … is there a way to do this? Sorry, but my php knowledge is very basic. Furthermore, I’m wondering if someone knows of a good resource how to present data through APIs - especially Github API (apart from official docs).

Thx a lot,

These methods can only be used with instances of a class, filterBy() etc., for example, are methods of the Collection class. You can’t use them with arrays. You can, however, use standard PHP functions to manipulate arrays (or use the methods of the toolkit A class).

1 Like

A class is just perfect, thanks a lot!

Collections class is awesome you can new up one with custom data pretty easily:

$people = new collection([
  ['name' => 'Bastian'],
  ['name' => 'Sonja'],
  ['name' => 'Pedro'],
  ['name' => 'Hjørdis']

// var_dump($people->count());
// var_dump($people->first());
// var_dump($people->pluck('name'));
// var_dump($people->sortBy('name'));
// var_dump($people->findBy('name', 'Bastian'));

how would I feed it my array, by passing it as variable to new collection($my_array)?

Yes, you can pass a variable that references an array or pass the array directly as I showed.

That made things super-clear, thank you!