如何导出PHP数组,其中每个键值对是单独的行? [英] How to export PHP array where each key-value pair is in separate line?

查看:399
本文介绍了如何导出PHP数组,其中每个键值对是单独的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找等同的功能 var_export()这让我的PHP数组导出为可解析code,但每个语句应该在单独的行打印(所以每个线具有其自己独立的结构)。

I'm looking for equivalent functionality to var_export() which allows me to export PHP array into parseable code, but each statement should be printed in separate line (so each line has its own independent structure).

目前此code:

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export($a);
?>

将输出:

array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)

不过我期待输出到以下格式如下:

However I'm looking to output into the following format like:

$foo = array()
$foo['0'] = 1
$foo['1'] = 2
$foo['2'] = array();
$foo['2']['0'] = 'a';
$foo['2']['1'] = 'b';
$foo['2']['2'] = 'c';

所以它的执行将导致相同的原始数组

so execution of it will result in the same original array.

我们的目标是管理人类理解的格式非常大​​的数组,这样你就可以很容易地取消设置一些选定的项目仅仅通过复制和放大器;粘贴方法(其中每行包括完整路径的元素)。通常,当你倾倒在屏幕上非常大的阵列,问题是,你已经保持向上滚动很长的时间才能找到自己的父亲的父亲,这几乎是不可能找出哪些元素属于哪个,什么是他们不浪费完整路径多的时间。

The goal is to manage very large arrays in human understandable format, so you can easily unset some selected items just by copy & paste method (where each line includes the full path to the element). Normally when you dump very big array on the screen, the problem is that you've to keep scrolling up very long time to find its parent's parent and it's almost impossible to find out which element belongs to which and what is their full path without wasting much amount of time.

推荐答案

目前我已经的这里发现(发表的 ravenswd 的)这个简单的功能,能够做到这一点:

Currently I've found here (posted by ravenswd) this simple function which can achieve that:

function recursive_print ($varname, $varval) {
  if (! is_array($varval)):
    print $varname . ' = ' . var_export($varval, true) . ";<br>\n";
  else:
    print $varname . " = array();<br>\n";
    foreach ($varval as $key => $val):
      recursive_print ($varname . "[" . var_export($key, true) . "]", $val);
    endforeach;
  endif;
}

输出,适合 recursive_print('$ A',$ A);

$a = array();
$a[0] = 1;
$a[1] = 2;
$a[2] = array();
$a[2][0] = 'a';
$a[2][1] = 'b';
$a[2][2] = 'c';

这篇关于如何导出PHP数组,其中每个键值对是单独的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆