php 多维数组转字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12309047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:19:13  来源:igfitidea点击:

Multidimensional Array to String

php

提问by Niet the Dark Absol

I am trying to convert a multidimensional array into a string with a particular format.

我正在尝试将多维数组转换为具有特定格式的字符串。

function convert_multi_array($array) {
    foreach($array as $value) {
        if(count($value) > 1) {
            $array = implode("~", $value);
        }
        $array = implode("&", $value);
    }
    print_r($array);
}
$arr = array(array("blue", "red", "green"), array("one", "three", "twenty"));
convert_multi_array($arr);

Should Output: blue~red~green&one~three~twenty... and so on for more sub-arrays.

应该输出:blue~red~green&one~three~twenty...等更多子数组。

Let me just say that I have not been able to produce any code that is remotely close to the results I want. After two hours, this is pretty much the best I can get. I don't know why the implodes are acting differently than they usually do for strings or maybe I'm just not looking at this right. Are you able to use implode for arrays values?

我只想说,我无法生成任何与我想要的结果非常接近的代码。两个小时后,这几乎是我能得到的最好的结果。我不知道为什么内爆的行为与通常对字符串的行为不同,或者也许我只是没有正确看待这一点。你能对数组值使用内爆吗?

回答by Niet the Dark Absol

You are overwriting $array, which contains the original array. But in a foreacha copy of $arrayis being worked on, so you are basically just assigning a new variable.

您正在覆盖$array,其中包含原始数组。但是正在处理foreach的副本中$array,因此您基本上只是分配一个新变量。

What you should do is iterate through the child arrays and "convert" them to strings, then implode the result.

您应该做的是遍历子数组并将它们“转换”为字符串,然后内爆结果。

function convert_multi_array($array) {
  $out = implode("&",array_map(function($a) {return implode("~",$a);},$array));
  print_r($out);
}

回答by Jeff_Alieffson

Look at my version. It implodes any dimension:

看我的版本。它内爆任何维度:

function implode_all($glue, $arr){            
    for ($i=0; $i<count($arr); $i++) {
        if (@is_array($arr[$i])) 
            $arr[$i] = implode_all ($glue, $arr[$i]);
    }            
    return implode($glue, $arr);
}

Here is the example:

这是示例:

echo implode_all(',', 
array(
1,
2,
array('31','32'),
array('4'),
array(
  array(511,512), 
  array(521,522,523,524),
  53))
);

Will print:

将打印:

1,2,31,32,4,511,512,521,522,523,524,53

回答by kachmi

Here is a simple answer:

这是一个简单的答案:

    function implode_recur ($separator, $arrayvar){
        $output = "";
         foreach ($arrayvar as $av)
         {
             if (is_array ($av))
             {
                $output .= implode_r ($separator, $av);
             }
            else
            {
                $output .= $separator.$av;
            }
            return $output;
         }
    }
    $result = implode_recur(">>",$variable);

Okay happy coding!

好的,编码愉快!

回答by desimusxvii

Saving the imploded inner arrays to an array. Then imploding those.

将内爆的内部数组保存到数组中。然后内爆那些。

(written in the spirit of your original implementation)

(本着您最初实现的精神编写的)

function convert_multi_array($arrays)
{
    $imploded = array();
    foreach($arrays as $array) {
        $imploded[] = implode('~', $array);
    }
    return implode("&", $imploded);
}

回答by Mark

You also can just take the original implementation from the PHP join manual (http://php.net/manual/en/function.join.php)

您也可以从 PHP 连接手册中获取原始实现(http://php.net/manual/en/function.join.php

function joinr($join, $value, $lvl=0)
{
    if (!is_array($join)) return joinr(array($join), $value, $lvl);
    $res = array();
    if (is_array($value)&&sizeof($value)&&is_array(current($value))) { // Is value are array of sub-arrays?
        foreach($value as $val)
            $res[] = joinr($join, $val, $lvl+1);
    }
    elseif(is_array($value)) {
        $res = $value;
    }
    else $res[] = $value;
    return join(isset($join[$lvl])?$join[$lvl]:"", $res);
}

$arr = array(array("blue", "red", "green"), array("one", "three", "twenty"));
joinr(array("&", "~"), $arr);

This is recursive, so you even can do

这是递归的,所以你甚至可以做

$arr = array(array("a", "b"), array(array("c", "d"), array("e", "f")), "g");
joinr(array("/", "-", "+"), $arr);

and you'll get

你会得到

a-b/c+d-e+f/g

回答by Chris Field

May be of no interest, but i needed to report a nested array which contained errors, so i simply defined an variable and then with json_encode put this in my email. One way of converting a array to a string.

可能没有兴趣,但我需要报告一个包含错误的嵌套数组,所以我只是定义了一个变量,然后用 json_encode 把它放在我的电子邮件中。将数组转换为字符串的一种方法。

回答by Irshad Khan

You can use this one line array to string code:

您可以使用这个单行数组来串接代码:

$output = implode(", ",array_map(function($a) { return '<a href="'.$a['slug'].'">'.$a['name'].'</a>'; }, $your_array) );

print_r($output);

Output:

输出:

Link1, Link2, Link3

链接 1、链接 2、链接 3