This repository was archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.php
125 lines (113 loc) · 2.72 KB
/
examples.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Flat.php examples
* https://github.com/TheNocoder/FlatPHP
* Copyright (c) 2023
* https://github.com/TheNocoder/FlatPHP/LICENSE
*/
include 'Flat.php';
/*
* Default options
*/
$flattened = [];
$unflattened = [];
$sampleArray = [
'id' => 12345,
'name' => 'A name',
'properties' => [
'type' => 'A type',
'geo' => [
'latitude' => 12.3456,
'longitude' => 12.3456
],
'collections' => [
[true, false],
['a', 'b']
]
],
'empty' => [[]],
];
echo "<pre>";
echo "\nDefault options.";
flatten_array($sampleArray, $flattened);
unflatten_array($flattened, $unflattened);
echo "\nsampleArray to flattened:\n";
print_r($flattened);
echo "\nflattened to JSON:\n";
print_r(json_encode($flattened, JSON_PRETTY_PRINT));
echo "\n";
echo "\nflattened to unflattened:\n";
print_r($unflattened);
/*
* Custom prefix for suffix
*/
$flattened = [];
$unflattened = [];
$options = [
'prefix' => '{', // prefix
'suffix' => '}', // suffix
'suffix-end' => true, // ends with $suffix
'prefix-list' => '[', // prefix if array list
'suffix-list' => ']', // suffix if array list
'suffix-list-end' => true, // ends with $sufixList if array list
];
echo "\nflattened by options:\n";
print_r($options);
flatten_array($sampleArray, $flattened, $options);
print_r($flattened);
/*
* Custom suffix in all cases
*/
$flattened = [];
$unflattened = [];
$options = [
'prefix' => '',
'suffix' => '->',
'suffix-end' => false,
'prefix-list' => '',
'suffix-list' => '',
];
echo "\nflattened by options:\n";
print_r($options);
flatten_array($sampleArray, $flattened, $options);
print_r($flattened);
/*
* Custom start
*/
$flattened = [];
$unflattened = [];
$start = 'https://example.com/';
$options = [
'prefix' => '',
'suffix' => '/',
'suffix-end' => true,
'prefix-list' => '',
'suffix-list' => '',
];
echo "\nflattened \$start='$start' by options:\n";
print_r($options);
flatten_array($sampleArray, $flattened, $options, $start);
print_r($flattened);
/*
* Performance
*/
$flattened = [];
$unflattened = [];
$inter = 1000;
$start = microtime(true);
for ($n = 0; $n < $inter; $n++) {
flatten_array($sampleArray, $flattened);
}
$end = microtime(true);
echo "\nPerformance flatten_array ($inter interactions):\n";
echo ($end - $start) * 1000;
echo " ms.\n";
$start = microtime(true);
for ($n = 0; $n < $inter; $n++) {
unflatten_array($flattened, $unflattened);
}
$end = microtime(true);
echo "\nPerformance unflatten_array ($inter interactions):\n";
echo ($end - $start) * 1000;
echo " ms.\n";
echo "</pre>";