Skip to content

Commit 16dab2b

Browse files
[12.x] Add Arr::from() to helpers (#10394)
* add `Arr::from()` to helpers * Update helpers.md * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 3a19b52 commit 16dab2b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

helpers.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
5252
[Arr::flatten](#method-array-flatten)
5353
[Arr::float](#method-array-float)
5454
[Arr::forget](#method-array-forget)
55+
[Arr::from](#method-array-from)
5556
[Arr::get](#method-array-get)
5657
[Arr::has](#method-array-has)
5758
[Arr::hasAny](#method-array-hasany)
@@ -491,6 +492,27 @@ Arr::forget($array, 'products.desk');
491492
// ['products' => []]
492493
```
493494

495+
<a name="method-array-from"></a>
496+
#### `Arr::from()` {.collection-method}
497+
498+
The `Arr::from` method converts various input types into a plain PHP array. It supports a range of input types, including arrays, objects, and several common Laravel interfaces, such as `Arrayable`, `Enumerable`, `Jsonable`, and `JsonSerializable`. Additionally, it handles `Traversable` and `WeakMap` instances:
499+
500+
```php
501+
use Illuminate\Support\Arr;
502+
503+
Arr::from((object) ['foo' => 'bar']); // ['foo' => 'bar']
504+
505+
class TestJsonableObject implements Jsonable
506+
{
507+
public function toJson($options = 0)
508+
{
509+
return json_encode(['foo' => 'bar']);
510+
}
511+
}
512+
513+
Arr::from(new TestJsonableObject); // ['foo' => 'bar']
514+
```
515+
494516
<a name="method-array-get"></a>
495517
#### `Arr::get()` {.collection-method}
496518

0 commit comments

Comments
 (0)