In Laravel 11.x & Laravel 12.x, the `Arr::join()` method is a utility provided by the `Illuminate\Support\Arr` class. It is used to concatenate array elements into a single string, with an optional delimiter between the elements. This method is particularly useful when you need to combine array values into a readable or formatted string.
How Arr::join() Works?
1. Basic Join
Join array elements with a comma and space:
<?php use Illuminate\Support\Arr; $array = ['apple', 'banana', 'cherry']; $result = Arr::join($array, ', '); echo $result; // Output: "apple, banana, cherry" ?>
2. Join Associative Arrays
`Arr::join()` works with both indexed and associative arrays. For associative arrays, it joins the values:
<?php use Illuminate\Support\Arr; $array = ['fruit1' => 'apple', 'fruit2' => 'banana', 'fruit3' => 'cherry']; $result = Arr::join($array, ', ', ' and '); echo $result; // Output: "apple, banana and cherry" ?>
3. Join with Empty Array
If the array is empty, `Arr::join()` returns an empty string:
<?php use Illuminate\Support\Arr; $array = []; $result = Arr::join($array, ', '); echo $result; // Output: "" ?>