Home > Backend Development > PHP Tutorial > Improve PHP's var_dump method to adapt it to display data retrieved from the database

Improve PHP's var_dump method to adapt it to display data retrieved from the database

WBOY
Release: 2016-07-30 13:30:18
Original
927 people have browsed it

This is an improved function that outputs an array structure to facilitate the display of data retrieved from the database.

/**
     * array(7) {
        [0] => array(32) {
            ["order_id"] => string(2) "34"
            ["shipping_time"] => string(1) "0"
            ["created_at"] => string(10) "1433565988"
        }
        [1] => array(32) {
            ["order_id"] => string(2) "35"
            ["shipping_time"] => string(1) "0"
            ["created_at"] => string(10) "1433573933"
        }
        [2] => array(32) {
            ["order_id"] => string(2) "36"
            ["order_sn"] => string(9) "BJA000036"
            ["wx_trade_no"] => string(32) "wx712cd30713b968c114336440083628"
        }
     * 形如上面这样的数组,使用var_dump()函数打印的时候会打印出所有的数据项,尤其是在数据库字段较多的情况下,
     * 不便于找到自己感兴趣的数据。
     * 本方法改进了var_dump()方法,可以指定要打印的键。例如想看从order表中查处的order_id和city_id,
     * 可以这样使用:du($order,['order_id','city_id'])
     * 输出如下格式:
     *array[7] {
        [0] =>array[32] {
            [order_id] => string(2) : 34
            [city_id] => string(1) : 0
        }
        [1] =>array[32] {
            [order_id] => string(2) : 35
            [city_id] => string(1) : 0
        }
        [2] =>array[32] {
            [order_id] => string(2) : 36
            [city_id] => string(1) : 0
        }
        [3] =>array[32] {
            [order_id] => string(2) : 37
            [city_id] => string(1) : 0
        }
     }
     * @param $data 要显示结构的变量
     * @param $keys 要显示的键,为空则显示所有的键
     * @param $echo 是否输出结果到浏览器
     */
    public function du($data,$keys='',$echo=true)
    {
        $message = '';
        $message .= gettype($data) . "[" . count($data) . "] {" . "<br>";
        foreach ($data as $cc => $c) {
            $message .= " [" . $cc . "] =>" . gettype($c) . "[" . count($c) . "] {" . "<br>";
            foreach ($c as $key => $value) {
                if (empty($keys)) {
                    $message .= "  [" . $key . "]" . " => " . gettype($value) . "(" . strlen($value) . ")" . " : <span style=&#39;color:blue&#39;>" . $value . "</span>";
                    $message .= "<br>";
                } else if (in_array($key, $keys)) {
                    $message .= "  [" . $key . "]" . " => " . gettype($value) . "(" . strlen($value) . ")" . " : <span style=&#39;color:blue&#39;>" . $value . "</span>";
                    $message .= "<br>";
                }
            }
            $message .= "  }";
            $message .= "<br>";
        }
        $message .= "}";
        if ($echo) {
            echo $message;
        } else {
            return $message;
        }
    }
Copy after login
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced how to improve PHP's var_dump method to adapt it to display the data retrieved from the database, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template