回答編集履歴
4
修正
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
**[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
|
22
22
|
|
23
|
-
マニュアルの User Contributed Notes の print_r_reverse を少し加工することでイケそうです。
|
23
|
+
[マニュアル](https://www.php.net/manual/ja/function.print-r.php)の User Contributed Notes の print_r_reverse() を少し加工することでイケそうです。
|
24
24
|
|
25
25
|
```php
|
26
26
|
|
3
追記
test
CHANGED
@@ -19,3 +19,191 @@
|
|
19
19
|
|
20
20
|
|
21
21
|
**[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
|
22
|
+
|
23
|
+
マニュアルの User Contributed Notes の print_r_reverse を少し加工することでイケそうです。
|
24
|
+
|
25
|
+
```php
|
26
|
+
|
27
|
+
<?php
|
28
|
+
|
29
|
+
class Test
|
30
|
+
|
31
|
+
{
|
32
|
+
|
33
|
+
public $a = [
|
34
|
+
|
35
|
+
[
|
36
|
+
|
37
|
+
"b" => 5,
|
38
|
+
|
39
|
+
"c" => 100,
|
40
|
+
|
41
|
+
],
|
42
|
+
|
43
|
+
];
|
44
|
+
|
45
|
+
public $d = 0;
|
46
|
+
|
47
|
+
public $e =1;
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
$test = new Test;
|
52
|
+
|
53
|
+
$tmp = print_r($test,true);
|
54
|
+
|
55
|
+
print_r(print_r_reverse($tmp));
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
function print_r_reverse($input) {
|
62
|
+
|
63
|
+
$lines = preg_split('#\r?\n#', trim($input));
|
64
|
+
|
65
|
+
if (trim($lines[ 0 ]) != 'Array' && trim($lines[ 0 ] != 'Test Object')) {
|
66
|
+
|
67
|
+
// bottomed out to something that isn't an array or object
|
68
|
+
|
69
|
+
if ($input === '') {
|
70
|
+
|
71
|
+
return null;
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
return $input;
|
78
|
+
|
79
|
+
} else {
|
80
|
+
|
81
|
+
// this is an array or object, lets parse it
|
82
|
+
|
83
|
+
$match = array();
|
84
|
+
|
85
|
+
if (preg_match("/(\s{5,})(/", $lines[ 1 ], $match)) {
|
86
|
+
|
87
|
+
// this is a tested array/recursive call to this function
|
88
|
+
|
89
|
+
// take a set of spaces off the beginning
|
90
|
+
|
91
|
+
$spaces = $match[ 1 ];
|
92
|
+
|
93
|
+
$spaces_length = strlen($spaces);
|
94
|
+
|
95
|
+
$lines_total = count($lines);
|
96
|
+
|
97
|
+
for ($i = 0; $i < $lines_total; $i++) {
|
98
|
+
|
99
|
+
if (substr($lines[ $i ], 0, $spaces_length) == $spaces) {
|
100
|
+
|
101
|
+
$lines[ $i ] = substr($lines[ $i ], $spaces_length);
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
$is_object = trim($lines[ 0 ]) == 'stdClass Object';
|
110
|
+
|
111
|
+
array_shift($lines); // Array
|
112
|
+
|
113
|
+
array_shift($lines); // (
|
114
|
+
|
115
|
+
array_pop($lines); // )
|
116
|
+
|
117
|
+
$input = implode("\n", $lines);
|
118
|
+
|
119
|
+
$matches = array();
|
120
|
+
|
121
|
+
// make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
|
122
|
+
|
123
|
+
preg_match_all("/^\s{4}[(.+?)] \=\> /m", $input, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
|
124
|
+
|
125
|
+
$pos = array();
|
126
|
+
|
127
|
+
$previous_key = '';
|
128
|
+
|
129
|
+
$in_length = strlen($input);
|
130
|
+
|
131
|
+
// store the following in $pos:
|
132
|
+
|
133
|
+
// array with key = key of the parsed array's item
|
134
|
+
|
135
|
+
// value = array(start position in $in, $end position in $in)
|
136
|
+
|
137
|
+
foreach ($matches as $match) {
|
138
|
+
|
139
|
+
$key = $match[ 1 ][ 0 ];
|
140
|
+
|
141
|
+
$start = $match[ 0 ][ 1 ] + strlen($match[ 0 ][ 0 ]);
|
142
|
+
|
143
|
+
$pos[ $key ] = array($start, $in_length);
|
144
|
+
|
145
|
+
if ($previous_key != '') {
|
146
|
+
|
147
|
+
$pos[ $previous_key ][ 1 ] = $match[ 0 ][ 1 ] - 1;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
$previous_key = $key;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
$ret = array();
|
156
|
+
|
157
|
+
foreach ($pos as $key => $where) {
|
158
|
+
|
159
|
+
// recursively see if the parsed out value is an array too
|
160
|
+
|
161
|
+
$ret[ $key ] = print_r_reverse(substr($input, $where[ 0 ], $where[ 1 ] - $where[ 0 ]));
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
return $is_object ? (object)$ret : $ret;
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
?>
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
Array
|
180
|
+
|
181
|
+
(
|
182
|
+
|
183
|
+
[a] => Array
|
184
|
+
|
185
|
+
(
|
186
|
+
|
187
|
+
[0] => Array
|
188
|
+
|
189
|
+
(
|
190
|
+
|
191
|
+
[b] => 5
|
192
|
+
|
193
|
+
[c] => 100
|
194
|
+
|
195
|
+
)
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
[d] => 0
|
204
|
+
|
205
|
+
[e] => 1
|
206
|
+
|
207
|
+
)
|
208
|
+
|
209
|
+
```
|
2
修正
test
CHANGED
@@ -18,6 +18,4 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
**[追記]
|
21
|
+
**[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
|
22
|
-
|
23
|
-
[print_r の User Contributed Notes ](https://php.net/manual/en/function.print-r.php)に print_r_reverse() の記述があります。
|
1
追記
test
CHANGED
@@ -13,3 +13,11 @@
|
|
13
13
|
**いろいろ事情はあるでしょうが、個人的には、出力形式を変更するのが適切だと思います。**
|
14
14
|
|
15
15
|
serialize(), var_export(), json_encode() あたりを使用すれば、再利用性は高まります。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
**[追記]失礼。print_r() ですね。var_dump() 前提で回答してました^^;**
|
22
|
+
|
23
|
+
[print_r の User Contributed Notes ](https://php.net/manual/en/function.print-r.php)に print_r_reverse() の記述があります。
|