2015-09-15 81 views
2

我想将多维数组转换为字符串。PHP:将多维数组转换为字符串

到目前为止,我已经能够将管道分隔字符串转换为数组。

如:

group|key|value 
group|key_second|value 

将呈现为以下阵列:

$x = array(
    'group' => array(
     'key' => 'value', 
     'key_second' => 'value' 
    ), 
); 

不过,现在我希望它是周围的其他方法,其中提供了多维数组,我想将它转换为管道分隔字符串,就像在第一个代码示例中一样。

任何想法如何做到这一点?


PS:请大家注意,该阵列可以动态有任何深度。

例如:

$x['group']['sub_group']['category']['key'] = 'value'

转化为

group|sub_group|category|key|value

+0

你已经试过什么

function array_to_pipe($array, $delimeter = '|', $parents = array(), $recursive = false) { $result = ''; foreach ($array as $key => $value) { $group = $parents; array_push($group, $key); // check if value is an array if (is_array($value)) { if ($merge = array_to_pipe($value, $delimeter, $group, true)) { $result = $result . $merge; } continue; } // check if parent is defined if (!empty($parents)) { $result = $result . PHP_EOL . implode($delimeter, $group) . $delimeter . $value; continue; } $result = $result . PHP_EOL . $key . $delimeter . $value; } // somehow the function outputs a new line at the beginning, we fix that // by removing the first new line character if (!$recursive) { $result = substr($result, 1); } return $result; } 

演示到目前为止,你可以请更新一个.. –

+0

阅读php手册http://php.ne上的'implode'部分t/manual/en/function.implode.php –

+0

implode对于单维数组很有用,但在这种情况下,这是一个多维数组,并且我需要数组键作为输出的一部分 – Alexecus

回答

2

我已经创建了自己的功能: 这应该没有问题,即使要处理大阵在此提供http://ideone.com/j6nThF

+0

很棒的功能。唯一的问题是,这个函数总是更深入,所以如果你有多个入口深入,它不会回来遍历第二个条目。 – carla

0

可能重复你可以做到这一点,如果你特别想要一个字符串:

$x = array(
'group' => array(
    'key' => 'value', 
    'key_second' => 'value' 
), 
'group2' => array(
    'key2' => 'value', 
    'key_second2' => 'value' 
), 
); 
$str=''; 
foreach ($x as $key=>$value) 
{ 
    if($str=='') 
     $str.=$key; 
    else 
     $str.="|$key"; 
    foreach ($value as $key1=>$value1) 
     $str.="|$key1|$value1"; 

} 

echo $str; //it will print group|key|value|key_second|value|group2|key2|value|key_second2|value 
1

你也可以做到这一点使用一个循环是这样的:

$x = array(
'group' => array(
    'key' => 'value', 
    'key_second' => 'value' 
) 
); 
$yourstring =""; 
foreach ($x as $key => $value) 
{ 
    foreach ($x[$key] as $key2 => $value2) 
    { 
     $yourstring .= $key.'|'.$key2.'|'.$x[$key][$key2]."<BR />"; 
    } 
} 

echo $yourstring; 

这里是一个工作DEMO

+1

嗨,我检查了你的演示。它适用于固定深度的数组,在这种情况下是2级。它应该适用于任何深度。 – Alexecus

1

此代码应做的事。

您需要一个递归函数来执行此操作。但要小心,不要传入对象或一个巨大的数组,因为这种方法非常耗费内存。

function reconvert($array,$del,$path=array()){ 
    $string=""; 
    foreach($array as $key=>$val){ 
     if(is_string($val) || is_numeric($val)){ 
      $string.=implode($del,$path).$del.$key.$del.$val."\n"; 
     } else if(is_bool($val)){ 
      $string.=implode($del,$path).$del.$key.$del.($val?"True":"False")."\n"; 
     } else if(is_null($val)){ 
      $string.=implode($del,$path).$del.$key.$del."NULL\n"; 
     }else if(is_array($val)=='array') { 
      $path[]=$key; 
      $string.=reconvert($val,$del,$path); 
      array_pop($path); 
     } else { 
      throw new Exception($key." has type ".gettype($val).' which is not a printable value.'); 
     } 
    } 
    return $string; 
} 

DEMO:http://ideone.com/89yLLo

+0

很好地完成。我试图测试你的代码,并且似乎在第一个条目上添加了分隔符。看到这里http://ideone.com/ZlOLEq – Alexecus

+0

啊,是的,没有想到这种情况。这是一个固定的代码。 http://ideone.com/oypu7F –