你用哪个函数查看数据结构
当一个项目中存在很多相关联的数据时,我们通常的做法是将其整理为一个数组。在开发阶段我们需要随时关注数组的结构与数据,这时就要用到PHP打印数组的几个函数(print_r,var_dump,var_export),到底哪个比较适合您当前的需求,看下文!
假如我们有如下一个数组(友情链接数组):
- $links = array(
- 0 => array(
- "url" => "www.phplamp.com",
- "title" => "phplamp站",
- "target" => "_blank",
- "type" => "font",
- ),
- 1 => array(
- "url" => "www.phplamp.org",
- "title" => "phplamp博客站",
- "target" => "_blank",
- "type" => "font",
- ),
- );
一、print_r() : 清晰的列出数组的讯息,数组的索引用“[]”包裹,使用此函数后数组的指针会移到最后。
- echo "<pre>";
- print_r($links);
- echo "</pre>";
- /**
- * 返回结果
- Array
- (
- [0] => Array
- (
- [url] => www.phplamp.com
- [title] => phplamp站
- [target] => _blank
- [type] => font
- )
- [1] => Array
- (
- [url] => www.phplamp.org
- [title] => phplamp博客站
- [target] => _blank
- [type] => font
- )
- )
- */
二、var_dump() : 打印数组的详细结构与信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
- echo "<pre>";
- var_dump($links);
- echo "</pre>";
- /**
- * 返回结果
- array(2) {
- [0]=>
- array(4) {
- ["url"]=>
- string(15) "www.phplamp.com"
- ["title"]=>
- string(10) "phplamp站"
- ["target"]=>
- string(6) "_blank"
- ["type"]=>
- string(4) "font"
- }
- [1]=>
- array(4) {
- ["url"]=>
- string(15) "www.phplamp.org"
- ["title"]=>
- string(16) "phplamp博客站"
- ["target"]=>
- string(6) "_blank"
- ["type"]=>
- string(4) "font"
- }
- }
- */
三、var_export() : 输出或返回数组的结构信息,此信息为PHP合法的代码,简单的缓存中我经常用到此函数。
- echo "<pre>";
- var_export($links);
- echo "</pre>";
- /**
- * 返回结果
- array (
- 0 =>
- array (
- 'url' => 'www.phplamp.com',
- 'title' => 'phplamp站',
- 'target' => '_blank',
- 'type' => 'font',
- ),
- 1 =>
- array (
- 'url' => 'www.phplamp.org',
- 'title' => 'phplamp博客站',
- 'target' => '_blank',
- 'type' => 'font',
- ),
- )
- */
先记录这点,有空再补~!
- 上一篇:您的项目中存在如下代码吗?
- 下一篇:Flash图片新闻的实现方法及源码下载
