在 php 中用不同的胶水内爆多维数组

Implode multidimensional array with different glue in php

我有如下数组:

Array
(
    [22] => Array
        (
            [0] => 60
            [29] => Array
                (
                    [0] => 6
                )

            [30] => Array
                (
                    [0] => 5
                    [1] => 8
                )

            [31] => Array
                (
                    [0] => 7
                    [1] => 9
                    [2] => 14
                    [3] => 26
                )

        )

    [23] => 12
    [35] =>10
    [42] =>22
)

现在我想像

那样内爆数组
60[6][5||8][7||9||14||26]|12|10|22

我试过下面的代码:

$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);

但它没有用所需的胶水内爆

我该怎么做?

您可以使用 foreachimplode 来实现您的模式:

<?php
header('Content-Type: text/plain');
$arr = array();
$arr22 = array();
$arr22[0] = 60;
$arr22[29] = array(6);
$arr22[30] = array(5,8);
$arr22[31] = array(7,9,14,26);
$arr[22] = $arr22;
$arr[23] = 12;
$arr[35] = 10;
$arr[42] = 22;

$output = '';
foreach($arr as $entry) {
    if(!is_array($entry)) {
        $output .= $entry;
    } else {
        // array
        foreach($entry as $inner) {
            if(is_array($inner)) {
                $output .= '[' . implode('||', $inner) . ']';
            } else {
                $output .= $inner;
            }
        }
    }
    $output .= '|';
}
echo substr($output, 0, strlen($output) - 1);
?>

输出:

60[6][5||8][7||9||14||26]|12|10|22

您可以使用此代码

 <?php
$a= Array(
    22 => Array(
            0 => 60,
            29 => Array(
                    0 => 6
                ),

            30 => Array
                (
                    0 => 5,
                    1 => 8
                ),

            31 => Array
                (
                    0 => 7,
                    1 => 9,
                    2 => 14,
                    3 => 26
                ),

        ),

    23 => 12,
    35 =>10,
    42 =>22,
);
$string='';
foreach($a as $arr){
    if(is_array($arr)){
        foreach($arr as $array){
            if(is_array($array)){
                $string .= '['.implode("||",$array).']';
            }else{
                if($string!==''){ $string .= '|';}
                $string .= $array;
            }
        }
    }else{
        if($string!==''){ $string .= '|';}
        $string .= $arr;
    }

}
echo $string;die;

?>

输出将是

60[6][5||8][7||9||14||26]|12|10|22

没有 foreach 的预期结果。

$array = [
    22 => [
        0 => 60,
        29 => [
            0 => 6
        ],
        30 => [
            0 => 5,
            1 => 8
        ],
        31 => [
            0 => 7,
            1 => 9,
            2 => 14,
            3 => 26
        ]
    ],
    23 => 12,
    35 => 10,
    42 => 22
];

$result = implode('|', array_map(function($item)
        {
        return is_array($item) // convert sub array into string
                 ? implode('', array_map(function($inner_item) 
                    {
                    return is_array($inner_item) // convert inner array into string
                             ? '[' . implode('||', $inner_item) . ']'
                             : $inner_item;
                    }, $item))
                 : $item;
        }, $array));
var_dump($result);

因此,我们有 3 种类型的分隔符:'|' - 第一级,''(空) - 第二级,'||' - 第三级。

这应该适合你:

此处的胶水可根据您的需要进行配置,并且此逻辑建立在递归调用的基础上,因此这适用于任何级别的多维数组:

<?php
$arrVal =  array (
    '22' => array(
        '0' => 60,
        '29' => array(
            '0' => 6
        ),
        '30' => array (
            '0' => 5,
            '1' => 8
        ),
        '31' => array (
            '0' => 7,
            '1' => 9,
            '2' => 14,
            '3' => 26
        )
    ),
    '23' =>  12,
    '35' => 10,
    '42' => 22
);
//60[6][5||8][7||9||14||26]|12|10|22
$constructedValue = "";
$glue = "||";
echo $constructedValue = implodeMultiArr($arrVal,$glue);
function implodeMultiArr($arrVal,$glue) {
    $i = 0;
    $constructedValue = "";
    foreach ( $arrVal as $k=>$v) {
        if ( is_array($v) ) {
            $constructedValue .= !empty($constructedValue)  ? "[".implodeMultiArr($v,$glue)."]" : implodeMultiArr($v,$glue)."]" ;
        } else {
            $constructedValue .=  !empty($constructedValue)  ? $glue.$v : $v ;
        }
        $i++;
    }
    return $constructedValue;
}