By John Koster
The data_get
function is identical in behavior to both the array_get
and object_get
function. The difference is that the data_get
function will accept both objects and arrays as its $target
input value.
#Signature
The signature of the data_get
function is:
1function data_get(
2 $target,
3 $key,
4 $default = null
5);
#Example Use
If we had the following code in our application:
1$arrayValue = [
2 'green' => 'apple'
3];
4
5$objectValue = (object)$arrayValue;
6
7$arrayValueGet = data_get($arrayValue, 'green');
8$objectValueGet = data_get($objectValue, 'green');
9
10// Supplying a default value.
11$arrayValueDefault = data_get(
12 $arrayValue,
13 'Latin',
14 'language'
15);
16
17// Supplying a default value.
18$objectValueDefault = data_get(
19 $arrayValue,
20 'Greek',
21 'language'
22);
After the above code has executed, the variables would contain the following values:
Variable | Value |
---|---|
$arrayValueGet |
apple |
$objectValueGet |
apple |
$arrayValueDefault |
language |
$objectValueDefault |
language |
∎