質問するログイン新規登録

回答編集履歴

4

修正

2019/05/11 00:35

投稿

退会済みユーザー
answer CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
 
11
11
  **[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
12
- マニュアルの User Contributed Notes の print_r_reverse を少し加工することでイケそうです。
12
+ [マニュアル](https://www.php.net/manual/ja/function.print-r.php)の User Contributed Notes の print_r_reverse() を少し加工することでイケそうです。
13
13
  ```php
14
14
  <?php
15
15
  class Test

3

追記

2019/05/11 00:35

投稿

退会済みユーザー
answer CHANGED
@@ -8,4 +8,98 @@
8
8
  serialize(), var_export(), json_encode() あたりを使用すれば、再利用性は高まります。
9
9
 
10
10
 
11
- **[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
11
+ **[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
12
+ マニュアルの User Contributed Notes の print_r_reverse を少し加工することでイケそうです。
13
+ ```php
14
+ <?php
15
+ class Test
16
+ {
17
+ public $a = [
18
+ [
19
+ "b" => 5,
20
+ "c" => 100,
21
+ ],
22
+ ];
23
+ public $d = 0;
24
+ public $e =1;
25
+ }
26
+ $test = new Test;
27
+ $tmp = print_r($test,true);
28
+ print_r(print_r_reverse($tmp));
29
+
30
+
31
+ function print_r_reverse($input) {
32
+ $lines = preg_split('#\r?\n#', trim($input));
33
+ if (trim($lines[ 0 ]) != 'Array' && trim($lines[ 0 ] != 'Test Object')) {
34
+ // bottomed out to something that isn't an array or object
35
+ if ($input === '') {
36
+ return null;
37
+ }
38
+
39
+ return $input;
40
+ } else {
41
+ // this is an array or object, lets parse it
42
+ $match = array();
43
+ if (preg_match("/(\s{5,})(/", $lines[ 1 ], $match)) {
44
+ // this is a tested array/recursive call to this function
45
+ // take a set of spaces off the beginning
46
+ $spaces = $match[ 1 ];
47
+ $spaces_length = strlen($spaces);
48
+ $lines_total = count($lines);
49
+ for ($i = 0; $i < $lines_total; $i++) {
50
+ if (substr($lines[ $i ], 0, $spaces_length) == $spaces) {
51
+ $lines[ $i ] = substr($lines[ $i ], $spaces_length);
52
+ }
53
+ }
54
+ }
55
+ $is_object = trim($lines[ 0 ]) == 'stdClass Object';
56
+ array_shift($lines); // Array
57
+ array_shift($lines); // (
58
+ array_pop($lines); // )
59
+ $input = implode("\n", $lines);
60
+ $matches = array();
61
+ // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
62
+ preg_match_all("/^\s{4}[(.+?)] \=\> /m", $input, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
63
+ $pos = array();
64
+ $previous_key = '';
65
+ $in_length = strlen($input);
66
+ // store the following in $pos:
67
+ // array with key = key of the parsed array's item
68
+ // value = array(start position in $in, $end position in $in)
69
+ foreach ($matches as $match) {
70
+ $key = $match[ 1 ][ 0 ];
71
+ $start = $match[ 0 ][ 1 ] + strlen($match[ 0 ][ 0 ]);
72
+ $pos[ $key ] = array($start, $in_length);
73
+ if ($previous_key != '') {
74
+ $pos[ $previous_key ][ 1 ] = $match[ 0 ][ 1 ] - 1;
75
+ }
76
+ $previous_key = $key;
77
+ }
78
+ $ret = array();
79
+ foreach ($pos as $key => $where) {
80
+ // recursively see if the parsed out value is an array too
81
+ $ret[ $key ] = print_r_reverse(substr($input, $where[ 0 ], $where[ 1 ] - $where[ 0 ]));
82
+ }
83
+
84
+ return $is_object ? (object)$ret : $ret;
85
+ }
86
+ }
87
+ ?>
88
+ ```
89
+ ```
90
+ Array
91
+ (
92
+ [a] => Array
93
+ (
94
+ [0] => Array
95
+ (
96
+ [b] => 5
97
+ [c] => 100
98
+ )
99
+
100
+ )
101
+
102
+ [d] => 0
103
+ [e] => 1
104
+ )
105
+ ```

2

修正

2019/05/11 00:34

投稿

退会済みユーザー
answer CHANGED
@@ -8,5 +8,4 @@
8
8
  serialize(), var_export(), json_encode() あたりを使用すれば、再利用性は高まります。
9
9
 
10
10
 
11
- **[追記]失礼。print_r() ですね。var_dump() 前提で回答してました^^;**
11
+ **[追記]var_dump() 前提で回答してましたが違うようですね^^;失礼。**
12
- [print_r の User Contributed Notes ](https://php.net/manual/en/function.print-r.php)に print_r_reverse() の記述があります。

1

追記

2019/05/11 00:09

投稿

退会済みユーザー
answer CHANGED
@@ -5,4 +5,8 @@
5
5
 
6
6
 
7
7
  **いろいろ事情はあるでしょうが、個人的には、出力形式を変更するのが適切だと思います。**
8
- serialize(), var_export(), json_encode() あたりを使用すれば、再利用性は高まります。
8
+ serialize(), var_export(), json_encode() あたりを使用すれば、再利用性は高まります。
9
+
10
+
11
+ **[追記]失礼。print_r() ですね。var_dump() 前提で回答してました^^;**
12
+ [print_r の User Contributed Notes ](https://php.net/manual/en/function.print-r.php)に print_r_reverse() の記述があります。