The Blog

Laravel 5

Laravel 5

Laravel 5: Randomizing Element Order With shuffle

John Koster
John Koster
April 11, 2018

The shuffle method in Laravel can be used to shuffle the elements in an array. It takes advantage of PHP's built-in shuffle method. However, it's important to note that this method is not suitable for cryptographic purposes. In the provided example, the shuffle method is used to shuffle a deck of cards created using the crossJoin method.

Laravel 5

Laravel 5: Reducing a Multi-Dimensional Array to a Single Dimension Losing Keys With flatten

John Koster
John Koster
April 11, 2018

The flatten helper method takes a multi-dimensional array and transforms it into a new array with only one dimension. It recursively flattens an array, and you can control the maximum number of sub-arrays to traverse. The array_flatten function is a shortcut to calling Arr::flatten and is declared in the global namespace.

Laravel 5

Laravel 5: Removing Elements From an Array With forget

John Koster
John Koster
April 11, 2018

The forget helper method in Laravel's Arr class allows you to easily remove items from an array using dot notation for specifying keys. This method modifies the original array directly, so there's no need to reassign the modified array to a new variable. You can also use the array_forget function as a shortcut to calling Arr::forget.

Laravel 5

Laravel 5: Representing Multi-Dimensional Arrays in Dot Notation With dot

John Koster
John Koster
April 11, 2018

The dot helper method in Laravel allows you to convert a multi-dimensional array into an associative array where the keys are created from the nested keys separated by a dot. You can also prepend a value to all the newly generated keys by using the $prepend argument. Additionally, there is a global array_dot helper function available as a shortcut to calling Arr::dot in Laravel.

Laravel 5

Laravel 5: Retrieving, and Removing an Element From an Array With pull

John Koster
John Koster
April 11, 2018

The pull helper method in Laravel is used to retrieve and remove a value from an array. It can be used with a default value if the desired key does not exist. This method directly modifies the original array. An example use case is removing a specific day from an array of weekdays in PHP. The pull method can be accessed through the Arr class in Laravel, or through the global array_pull helper function.

Laravel 5

Laravel 5: Retrieving Elements from an Array With get

John Koster
John Koster
April 11, 2018

The get helper method allows developers to quickly retrieve items from an array using dot notation, without having to use PHP's array syntax. The method also supports a default value, which will be returned if the specified key is not found in the array. An alternative to using the get method is to use PHP's array access syntax and the isset function, but the dot notation code is shorter and easier to read. Additionally, there is a global array_get helper function available as a shortcut to calling Arr::get.

Laravel 5

Laravel 5: Retrieving Nested Array Values With pluck

John Koster
John Koster
April 11, 2018

The pluck helper method is used to retrieve specific values from an array. It can be used on arrays of objects or arrays of arrays. You can also use dot notation to retrieve values from nested arrays or objects. Additionally, there is a global array_pluck function available as a shortcut to Arr::pluck.

Laravel 5

Laravel 5: Retrieving Random Elements from an Array With random

John Koster
John Koster
April 11, 2018

The random method in PHP's Illuminate\Support\Arr class allows you to retrieve random items from an input array. You can specify the number of random items to be returned, and if not specified, it will return one random item. Be cautious when specifying a number greater than the array's length, as it will throw an exception. Note that the random method is not suitable for cryptographic purposes since it relies on PHP's array_rand function. There is also a global helper function, array_random, that serves as a shortcut to calling the Arr::random method.

Laravel 5

Laravel 5: Sorting Arrays With sort

John Koster
John Koster
April 11, 2018

The sort helper method allows you to sort an array based on some condition returned by a callback function. It iterates over the values in the array and uses the value returned by the callback to determine the sort order. You can also use the array_sort function as a shortcut to calling Arr::sort. Keep in mind that the array_sort function internally creates a new Collection object and should not be used in a certain way to avoid unnecessary object creation.

Laravel 5

Laravel 5: Splitting an Array Into It's Keys and Values with divide

John Koster
John Koster
April 11, 2018

The divide helper method in Laravel separates an array into two new arrays: one containing all the keys and the other containing all the values. This can be useful when you need to work with the keys and values separately. You can use the divide method by calling Arr::divide and passing in the array you want to divide. Additionally, Laravel provides a global array_divide helper function that is a shortcut to calling Arr::divide.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more