ずっと前から思っていたことだけれど、phpvar_dump がとてもわかりづらくいつまでたってもなじめない。もっと直感的にわかりやすいものがありそうなんだけれど・・・。はて?
まぁ、よくわからないので、そうなると自分で作るしかないわけで、同じ処理を関数内で何度も呼び出すだけのことなんじゃないだろうかと。
とりあえず var_dump させると下のような具合。これは WordPress によってデータベースに保存されているアップロードした画像のメタデータ。

array(1) {
  [0]=>
  array(5) {
    ["width"]=>
    int(1300)
    ["height"]=>
    int(750)
    ["file"]=>
    string(28) "2016/04/1603120634_xwing.jpg"
    ["sizes"]=>
    array(3) {
      ["thumbnail"]=>
      array(4) {
        ["file"]=>
        string(28) "1603120634_xwing-150x150.jpg"
        ["width"]=>
        int(150)
        ["height"]=>
        int(150)
        ["mime-type"]=>
        string(10) "image/jpeg"
      }
      ["medium"]=>
      array(4) {
        ["file"]=>
        string(28) "1603120634_xwing-500x288.jpg"
        ["width"]=>
        int(500)
        ["height"]=>
        int(288)
        ["mime-type"]=>
        string(10) "image/jpeg"
      }
      ["medium_large"]=>
      array(4) {
        ["file"]=>
        string(28) "1603120634_xwing-768x443.jpg"
        ["width"]=>
        int(768)
        ["height"]=>
        int(443)
        ["mime-type"]=>
        string(10) "image/jpeg"
      }
    }
    ["image_meta"]=>
    array(12) {
      ["aperture"]=>
      string(1) "0"
      ["credit"]=>
      string(0) ""
      ["camera"]=>
      string(0) ""
      ["caption"]=>
      string(0) ""
      ["created_timestamp"]=>
      string(1) "0"
      ["copyright"]=>
      string(0) ""
      ["focal_length"]=>
      string(1) "0"
      ["iso"]=>
      string(1) "0"
      ["shutter_speed"]=>
      string(1) "0"
      ["title"]=>
      string(0) ""
      ["orientation"]=>
      string(1) "0"
      ["keywords"]=>
      array(0) {
      }
    }
  }
}
PHP
CopyExpand

そんなにわかりづらくもないか。ふむ、型までわかるし。
まぁ、それはいいとして、もっとぱっと見て直感的にどこに何があるということがわかった方がわかりやすいのではないかと思うわけで。

とりあえず何をすればいいのか考えやすくするために、地道にネストさせた foreach を並べて見る。

<?php
    function echo_tree( $ary ) {

       if ( is_array( $ary ) ) {
            foreach ( $ary as $k0 => $v0 ) {
                if ( is_array( $v0 ) ) {
                    foreach ( $v0 as $k1 => $v1 ) {
                        if ( is_array( $v1 ) ) {
                            foreach ( $v1 as $k2 => $v2 ) {
                                if ( is_array( $v2 ) ) {
                                    foreach ( $v2 as $k3 => $v3 ) {
                                        if ( is_array( $v3 ) ) {
                                            foreach ( $v3 as $k4 => $v4 ) {
                                                if ( is_array( $v4 ) ) {
                                                    echo '<p>' . $k0 . '=>' . $k1 . '->' . $k2 . '->' . $k3 . '->' . $k4 . '->' . var_dump( $v4 ) . '</p>'; 
                                                } else {
                                                    echo '<p>' . $k0 . '=>' . $k1 . '->' . $k2 . '->' . $k3 . '->' . $k4 . '->' . $v4 . '</p>'; 
                                                }
                                            }
                                        } else {
                                            echo '<p>' . $k0 . '=>' . $k1 . '->' . $k2 . '->' . $k3 . '->' . $v3 . '</p>'; 
                                        }
                                    }
                                } else {
                                    echo '<p>' . $k0 . '=>' . $k1 . '->' . $k2 . '->' . $v2 . '</p>'; 
                                }
                            }
                        } else {
                            echo '<p>' . $k0 . '=>' . $k1 . '->' . $v1 . '</p>'; 
                        }
                    }
                } else {
                    echo '<p>' . $k0 . '=>' . $v0 . '</p>'; 
                }
            }
        } else {
            echo '<p>' . $ary . '</p>';
        }
    }
?>
PHP
CopyExpand

てな具合に階層の深さの分だけ、foreach のネストも必要だってことだと。めんどくさい!
と、いうことでこれを再帰呼び出しを使って書き直せばいいはず。しかし、再帰呼び出しってのは脳みそがウニになる。ぐるぐるまわってもうぐにゃぐにゃでぐでぐでになる。
まぁ、でも、とりあえずなんとか・・・。でも、きっともっと簡単にかけるに違いないけれど。まっいいか。

<?php
    function echo_array( $ary, $k = '' ) {
        $arw = '▸';

        if ( is_array( $ary ) ) {

            foreach ( $ary as $key => $val) {

                $tk = is_numeric( $key ) ? '[' . ( string )$key . ']' : '[\'' . $key . '\']';

                if ( is_array( $val ) ) {

                    $tmpk = $k . $tk;
                    echo_array( $val, $tmpk );
                } else {
                    echo '<p>' . $k . $tk . $arw . $val . '</p>';
                }
            }
        } else {
            echo '<p>' . $k . ( $k ? $arw : '') . $ary . '</p>'; 
        }
    }
?>
PHP
CopyExpand

表示の仕方は下のようにそのままコピー&ペーストで変数の指定に使えるような具合のものにした。

[0]['width']1300
[0]['height']750
[0]['file']2016/04/1603120634_xwing.jpg
[0]['sizes']['thumbnail']['file']1603120634_xwing-150x150.jpg
[0]['sizes']['thumbnail']['width']150
[0]['sizes']['thumbnail']['height']150
[0]['sizes']['thumbnail']['mime-type']▸image/jpeg
[0]['sizes']['medium']['file']1603120634_xwing-500x288.jpg
[0]['sizes']['medium']['width']500
[0]['sizes']['medium']['height']288
[0]['sizes']['medium']['mime-type']▸image/jpeg
[0]['sizes']['medium_large']['file']1603120634_xwing-768x443.jpg
[0]['sizes']['medium_large']['width']768
[0]['sizes']['medium_large']['height']443
[0]['sizes']['medium_large']['mime-type']▸image/jpeg
[0]['image_meta']['aperture']0
[0]['image_meta']['credit'][0]['image_meta']['camera'][0]['image_meta']['caption'][0]['image_meta']['created_timestamp']0
[0]['image_meta']['copyright'][0]['image_meta']['focal_length']0
[0]['image_meta']['iso']0
[0]['image_meta']['shutter_speed']0
[0]['image_meta']['title'][0]['image_meta']['orientation']0
PHP
CopyExpand

Leave a Reply!

JavaScript is necessary to send a comment.
You can edit and delete your comment if you input a edit key.
Edit key is necessary for attesting you when you edit and delete it.
The tag of HTML cannot be used in comment.
When you comment for the first time, it is displayed after the approval of the administrator.
Because I cannot speak English so much, it takes time to answer.
Required fields are marked *.

※Please enter more than 5 characters only alphabets.
※Edit or delete are possible for 2000 days after approval.

*

♠Simplistic Comment User Editable v4.0

♠When visitors leave comments on the site this site collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.
♠This site does not use cookie when visitors leave comments and commenter edit comment.
♠This site uses Akismet to reduce spam. Learn how your comment data is processed.

Comments feed

Trackback URL : https://strix.main.jp/wp-trackback.php?p=165000

Sanbanse Funabashi

Top

スクロールさせるか画像をクリックすると元に戻ります。