-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.php
52 lines (46 loc) · 1.34 KB
/
Tools.php
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
<?php namespace JC;
class Tools
{
public static function getKeysFromArrayOfHashes($hashes)
{
$keys = array_unique(
call_user_func_array(
'array_merge',
array_map(
function ($hash) {
return array_keys($hash);
},
$hashes
)
)
);
return $keys;
}
public static function getRowsFromArrayOfHashesAlignedOnKeys($hashes, $keys = null)
{
if (is_null($keys)) {
$keys = self::getKeysFromArrayOfHashes($hashes);
}
$rows = array_map(function ($hash) use ($keys) {
return array_map(
function ($key) use ($hash) {
if (array_key_exists($key, $hash)) {
return $hash[$key];
}
},
$keys
);
}, $hashes);
return $rows;
}
public static function getArrayFromHashesWithKeysAsHeaders($hashes, $headers = null)
{
if (is_null($headers)) {
$headers = self::getKeysFromArrayOfHashes($hashes);
}
$keys = $headers;
$rows = self::getRowsFromArrayOfHashesAlignedOnKeys($hashes, $keys);
array_unshift($rows, $headers);
return $rows;
}
}