-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotty.php
More file actions
58 lines (54 loc) · 1.65 KB
/
Copy pathDotty.php
File metadata and controls
58 lines (54 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Class Dotty
*
* A simple class to get a value, or values, from an array, or many arrays, using dot notation.
*
*/
class Dotty
{
/**
* Use dot notation to access a value from an array.
*
* @param string $value Dot notation accessor
* @param array $record Array of values to search
* @return string
*/
public static function getValue($value, array $record)
{
foreach (explode('.', $value) as $section) {
$record = &$record[$section];
}
return $record;
}
/**
* Pass in many dot notation values to retrieve from an array.
*
* @param array $arrayOfValues Array of dot notation values, key will be preserved.
* @param array $record Array of values to search
* @return array Array of found values
*/
public static function getValues(array $arrayOfValues, array $record)
{
$newRecord = [];
foreach ($arrayOfValues as $name => $value) {
$newRecord[$name] = self::getValue($value, $record);
}
return $newRecord;
}
/**
* Pass in many dot notation values to retrieve from multiple arrays.
*
* @param array $arrayOfValues Array of dot notation values, key will be preserved.
* @param array $arrayOfRecords Multiple arrays of values to search
* @return array Multiple arrays of found values
*/
public static function getValuesMultiple(array $arrayOfValues, array $arrayOfRecords)
{
$returnArray = [];
foreach ($arrayOfRecords as $record) {
$returnArray[] = self::getValues($arrayOfValues, $record);
}
return $returnArray;
}
}