-
Notifications
You must be signed in to change notification settings - Fork 0
/
Other.php
71 lines (68 loc) · 2.6 KB
/
Other.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php namespace Mreschke\Helpers;
/**
* Other misc helpers.
* @copyright 2014 Matthew Reschke
* @license http://mreschke.com/license/mit
* @author Matthew Reschke <mail@mreschke.com>
*/
class Other
{
/**
* Check if array is an associative array.
* @param array $arr
* @return boolean
*/
public static function isAssoc($arr)
{
#return (is_array($array) && (count($array)==0 || 0 !== count(array_diff_key($array, array_keys(array_keys($array))) )));
return (is_array($arr) && array_keys($arr) !== range(0, count($arr) - 1));
}
/**
* Collapse a basic subentity into the single level master entity
* @param \Illuminate\Support\Collection|array $data
* @return \Illuminate\Support\Collection|array
*/
public static function collapse($data)
{
foreach ($data as $i => $row) {
foreach ($row as $key => $value) {
$found = false;
if (is_array($value) || is_object($value)) {
if (is_array(head($value)) || is_object(head($value))) {
// Cannot handle multi level subeneity, only 1-1
if (is_array($data[$i])) {
$data[$i][$key] = '--Complex--';
} elseif (is_object($data[$i])) {
$data[$i]->$key = '--Complex--';
}
} else {
// Subeneity is single level
$found = true;
foreach ($value as $subkey => $subvalue) {
if (is_array($subvalue)) {
$data[$i][$key] = '--Complex--';
} elseif (is_object($subvalue)) {
$data[$i]->$key = '--Complex--';
} else {
$collapsedKey = "$key.$subkey";
if (is_array($value)) {
$data[$i][$collapsedKey] = $subvalue;
} elseif (is_object($value)) {
$data[$i]->$collapsedKey = $subvalue;
}
}
}
}
}
if ($found) {
if (is_array($value)) {
unset($data[$i][$key]);
} elseif (is_object($value)) {
unset($data[$i]->$key);
}
}
}
}
return $data;
}
}