Laravel Miscellaneous Helper Function: data_set

November 21, 2016 —John Koster

data_set(&$target, $key, $value, $overwrite = true)

The data_set helper function can be used to set the value of an item in an array or an object property using dot notation. It accepts a reference to a $target array or object; the $key (array key or object property name) of the data to set and the $value to be set. It also defines an optional $overwrite parameter (which is set to true by default). If $overwrite is set to true, any existing values for the $key will be replaced.

The following code example will show a very basic usage of the data_set function:

1<?php
2 
3$testArray = [
4 'first_name' => 'Jane',
5 'last_name' => 'Doe',
6 'age' => 26
7];
8 
9data_set($testArray, 'first_name', 'Jill');

Because the $overwrite property is set to true by default the first_name item will now contain the value Jill. If we modified the example to look like this:

1<?php
2 
3$testArray = [
4 'first_name' => 'Jane',
5 'last_name' => 'Doe',
6 'age' => 26
7];
8 
9// Notice that `$overwrite` is set to `false`.
10data_set($testArray, 'first_name', 'Jill', false);

The value of first_name would remain unchanged. Things get a little more interesting when setting nested properties:

1<?php
2 
3$testArray = [
4 'first_name' => 'Jane',
5 'last_name' => 'Doe',
6 'age' => 26
7];
8 
9data_set($testArray, 'occupation.name', 'Data Engineer');
10data_set($testArray, 'occupation.status', 'Employed');

After the above example has executed the $testArray variable would contain a value similar to the following output:

1array:4 [
2 "first_name" => "Jane"
3 "last_name" => "Doe"
4 "age" => 26
5 "occupation" => array:2 [
6 "name" => "Data Engineer"
7 "status" => "Employed"
8 ]
9]

We can also use the asterisk (*) wild card character to set multiple nested properties at once:

1<?php
2 
3$testArray = [
4 'first_name' => 'Jane',
5 'last_name' => 'Doe',
6 'age' => 26
7];
8 
9data_set($testArray, 'occupation.name', 'Data Engineer');
10data_set($testArray, 'occupation.status', 'Employed');
11 
12data_set($testArray, 'occupation.*', null);

After the above example has executed the $testArray variable would contain a value similar to the following output:

1array:4 [
2 "first_name" => "Jane"
3 "last_name" => "Doe"
4 "age" => 26
5 "occupation" => array:2 [
6 "name" => null
7 "status" => null
8 ]
9]

#Using data_set With Objects

There is no difference in using the data_set helper function with objects instead of arrays. The following example demonstrates how we could use the data_set function fill a newly created object. The example does not handle nested items.

1<?php
2 
3// Create a test array to work with.
4$testArray = [
5 'first_name' => 'Jane',
6 'last_name' => 'Doe',
7 'age' => 26
8];
9 
10// Create a test object to work with.
11$testObject = new stdClass;
12 
13foreach ($testArray as $key => $value) {
14 data_set($testObject, $key, $value);
15}

After the above example has executed the $testObject variable would contain a value similar to the following output:

1{
2 "first_name": "Jane"
3 "last_name": "Doe"
4 "age": 26
5}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.