The Blog

Laravel 5

Laravel 5: Adding a New Element to the Beginning of an Array With prepend

John Koster
John Koster
April 11, 2018

Learn how to use the prepend helper method in Laravel to add a new item to the beginning of an array. Control the key of the new item by specifying an optional parameter. The prepend method does not modify the original array and returns a modified copy.

Laravel 5

Laravel 5: Adding Elements to the Array with add

John Koster
John Koster
April 11, 2018

The add helper method allows you to add a key-value pair to an array if the key doesn't already exist. To use the add method, pass in the array, key, and value as arguments, and the method will return the updated array. Alternatively, you can use the global array_add function, which is a shortcut for calling Arr::add directly.

Laravel 5

Laravel 5: Adding or Setting New Array Element Values With set

John Koster
John Koster
April 11, 2018

The set method in Laravel is used to set values within an array, and it is the opposite of the forget method. This function modifies the original array. It uses dot notation for the key and can also be used to change the entire array by passing null as the key. There is also a global array_set helper function available as a shortcut to calling Arr::set.

Laravel 5

Laravel 5: Array Helper Functions

John Koster
John Koster
April 11, 2018

Learn about Laravel's helper functions for manipulating array data structures. These functions offer additional features beyond PHP's built-in array functions and are located within the "Illuminate\Support\Arr" static class. Most of these functions treat arrays as immutable, meaning changes return a copy of the original array while leaving the original array unchanged. Laravel's array functions also support "dot" notation for accessing items in an array, similar to JavaScript's dot notation for accessing object properties. Explore various Laravel array helper functions like adding elements, splitting arrays, collapsing multi-dimensional arrays, representing multi-dimensional arrays in dot notation, excluding and filtering array elements, reducing arrays, finding occurrences, checking if an array contains an element, adding or setting array element values, removing elements, and retrieving array values.

Laravel 5

Laravel 5: Checking if an Array Contains an Element With has

John Koster
John Koster
April 11, 2018

Learn how to use the has helper method in Laravel to check if a specific key exists in a nested array using dot notation. Discover how this method provides an easier and more readable alternative to using PHP's isset function. Additionally, find out how to use the global array_has helper function as a shortcut to calling the Arr::has method.

Laravel 5

Laravel 5: Checking if an Array is an Associative Array With isAssoc

John Koster
John Koster
April 11, 2018

The isAssoc helper method checks if a given array is an associative array. An array is considered associative if it does not have sequential numeric keys starting from zero. The method returns true if the array is associative, and false otherwise. The provided example demonstrates how the method determines if arrays are associative or not.

Laravel 5

Laravel 5: Collapsing a Multi-Dimensional Array to a Single Level With collapse

John Koster
John Koster
April 11, 2018

The collapse helper method in Laravel's Illuminate\Support\Arr class allows you to collapse a nested array into a single-level array. The method accepts an array as its parameter and returns a new array with the contents of all the nested arrays. The method can work with arrays that contain any data type. Keep in mind that the collapse method is not recursive, so it won't collapse arrays within nested arrays. However, the method can also be used on collections, including nested collections. Additionally, Laravel provides a global array_collapse helper function which is a shortcut to calling the Arr::collapse method.

Laravel 5

Laravel 5: Conditionally Retrieving Array Values With where

John Koster
John Koster
April 11, 2018

The where helper method in Laravel is used to filter an array based on a callback function. It iterates over each element in the array and executes the callback on each key-value pair. If the callback returns a value that evaluates to true, the key-value pair is kept in the filtered array. This method can be used to compare both array values and key-value pairs. Additionally, Laravel provides a global array_where helper function, which is a shortcut to calling Arr::where.

Laravel 5

Laravel 5: Creating Combinations of Elements With crossJoin

John Koster
John Koster
April 11, 2018

The crossJoin method in Laravel's Illuminate\Support\Arr class allows you to combine input arrays, resulting in an array containing all possible combinations of the input values. Through an example, let's say we want to create a deck of cards. We can use crossJoin to combine arrays representing card suits and card values, resulting in a deck of 52 cards. We can further customize the deck by using array_map to transform the deck into a new array containing values in the format of <CARD_VALUE> of <CARD_SUITE>. Another example is using crossJoin to generate test payloads for browser testing, allowing you to create test cases for input element names and XSS payloads.

Laravel 5

Laravel 5: Determining if an Array Contains an Element With exists

John Koster
John Koster
April 11, 2018

Learn how to use the exists helper method in Laravel. This method allows you to check if a key or index exists in an array. It is similar to PHP's array_key_exists function. The method requires two parameters, the array and the key. You can use any value that returns true from the Arr::accessible helper method as the array value. Check out the example use cases to see it in action.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more