The
data_set
function sets a value within a nested array or object using "dot" notation:<?php $data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200);
<?php function data_fill(&$target, $key, $value)
{
return data_set($target, $key, $value, false);
}
}
<?php if ($segments) {
foreach ($target as &$inner) {
data_set($inner, $segments, $value, $overwrite);
}
} elseif ($overwrite) {
foreach ($target as &$inner) {
<?php $target[$segment] = [];
}
data_set($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite || ! Arr::exists($target, $segment)) {
$target[$segment] = $value;
}
<?php $target->{$segment} = [];
}
data_set($target->{$segment}, $segments, $value, $overwrite);
} elseif ($overwrite || ! isset($target->{$segment})) {
$target->{$segment} = $value;
}
<?php $target = [];
if ($segments) {
data_set($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite) {
$target[$segment] = $value;
}
<?php return $data;
}
return data_set($data, $attribute, null, true);
}
<?php $this->assertEquals(
['foo' => 'bar', 'baz' => 'boom'],
data_set($data, 'baz', 'boom')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => 'bar', 'baz' => 'kaboom'],
data_set($data, 'baz', 'kaboom')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => [], 'baz' => 'kaboom'],
data_set($data, 'foo.*', 'noop')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => ['bar' => 'boom'], 'baz' => 'kaboom'],
data_set($data, 'foo.bar', 'boom')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => ['bar' => 'boom'], 'baz' => ['bar' => 'boom']],
data_set($data, 'baz.bar', 'boom')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => ['bar' => 'boom'], 'baz' => ['bar' => ['boom' => ['kaboom' => 'boom']]]],
data_set($data, 'baz.bar.boom.kaboom', 'boom')
);
}
<?php $this->assertEquals(
['foo' => []],
data_set($data, 'foo.*.bar', 'noop')
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => [], 'bar' => [['baz' => 'original'], []]],
data_set($data, 'bar', [['baz' => 'original'], []])
);
$this->assertEquals(
<?php $this->assertEquals(
['foo' => [], 'bar' => [['baz' => 'boom'], ['baz' => 'boom']]],
data_set($data, 'bar.*.baz', 'boom')
);
$this->assertEquals(