Laravel 5: Setting Array or Object Values With data_set

April 15, 2018 —John Koster

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.

#Signature

The signature of the data_set function is:

1function data_set(
2 &$target,
3 $key,
4 $value,
5 $overwrite = true
6);

#Example Use

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

1$testArray = [
2 'first_name' => 'Jane',
3 'last_name' => 'Doe',
4 'age' => 26
5];
6 
7data_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$testArray = [
2 'first_name' => 'Jane',
3 'last_name' => 'Doe',
4 'age' => 26
5];
6 
7// Notice that `$overwrite` is set to `false`.
8data_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$testArray = [
2 'first_name' => 'Jane',
3 'last_name' => 'Doe',
4 'age' => 26
5];
6 
7data_set(
8 $testArray,
9 'occupation.name',
10 'Data Engineer'
11);
12 
13data_set(
14 $testArray,
15 'occupation.status',
16 'Employed'
17);

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$testArray = [
2 'first_name' => 'Jane',
3 'last_name' => 'Doe',
4 'age' => 26
5];
6 
7data_set(
8 $testArray,
9 'occupation.name',
10 'Data Engineer'
11);
12 
13data_set(
14 $testArray,
15 'occupation.status',
16 'Employed'
17);
18 
19data_set(
20 $testArray,
21 'occupation.*',
22 null
23);

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

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.