You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure how to do this per the current implementation. each() only takes one parameter, the value. The key could always be the index in the event that special keys are not used.
The text was updated successfully, but these errors were encountered:
Currently each() works only on a collection. This means that if you call each() on an item (eg. a node containing an associative array or some primitive value) then it will wrap the item in a collection, so that each() receives the item.
To iterate key/value pairs would require a breaking change to the API that each() would not wrap an associative array or an iterable object, and then it could therefore receive $key as it's second argument - when called on a collection, this would be the sequential array index.
I think this would be a good change though, and it could coincide with implementing collection() and item():
// alternative to get() to force the type of node expected.
$arrayobj->collection('books.0'); // If referencing an item, automatically wrap in a collection
$arrayobj->item('books'); // If referencing a collection, automatically call first() to get the first item
// revised each()
$arrayobj->item('books')->each(function($val, $key) {
echo "$key: $val\n"; // eg. title: 1984
});
$arrayobj->collection('books.0')->each(function($book, $index) {
echo "$index: {$book->title}\n"; // eg. 0: 1984
});
Not sure how to do this per the current implementation.
each()
only takes one parameter, the value. The key could always be the index in the event that special keys are not used.The text was updated successfully, but these errors were encountered: