PHP Imploding多维数组

时间:2016-05-05 08:28:19

标签: php arrays

我需要使用implode在字符串中内爆一个多维数组,我尝试使用此处显示的array_mapstackoverflow.com但我失败了。

阵列:

Array ( 
    [0] => Array ( 
        [code] => IRBK1179 
        [qty] => 1 
    ) 
    [1] => Array ( 
        [code] => IRBK1178 
        [qty] => 1 
    ) 
    [2] => Array ( 
        [code] => IRBK1177 
        [qty] => 1 
    ) 
)

期望的输出:

IRBK1179:1|IRBK1178:1|IRBK1177:1

6 个答案:

答案 0 :(得分:3)

将foreach和implode()内部数组与:一起使用,然后将implode()新数组与|一起使用。请尝试以下代码。

$arr = Array ( 
        0 => Array ( 'code' => 'IRBK1179','qty' => 1 ),
        1 => Array ( 'code' => 'IRBK1178','qty' => 1 ),
        2 => Array ( 'code' => 'IRBK1177','qty' => 1 ) );
$newArr = array();
foreach ($arr as $row)
{
    $newArr[]= implode(":", $row);
}

echo $finalString = implode("|", $newArr);

<强>输出

IRBK1179:1|IRBK1178:1|IRBK1177:1

在线演示演示:Click Here

使用explode()从字符串中获取数组。

尝试以下代码。

$finalString = "IRBK1179:1|IRBK1178:1|IRBK1177:1";
$firstArray = explode("|", $finalString);
foreach($firstArray as $key=>$row)
{
    $tempArray = explode(":", $row);
    $newArray[$key]['code'] = $tempArray[0];
    $newArray[$key]['qty'] = $tempArray[1];
}

print_r($newArray);

<强>输出

Array
(
    [0] => Array
        (
            [code] => IRBK1179
            [qty] => 1
        )

    [1] => Array
        (
            [code] => IRBK1178
            [qty] => 2
        )

    [2] => Array
        (
            [code] => IRBK1177
            [qty] => 1
        )

)

工作演示:Click Here

答案 1 :(得分:3)

正如我所评论的那样,使用implodeforeach。对于内部数组使用:,对于外部数组使用|

$str = array();
foreach($arr as $val){
    $str[] = implode(":", $val);
}
echo implode("|", $str); //IRBK1179:1|IRBK1178:1|IRBK1177:1

答案 2 :(得分:2)

FrayneRuchish给出的其他答案都是正确的。

以下是使用array_map的另一种选择。

$arr = [[ 'code' => 'IRBK1179','qty' => 1 ],
        [ 'code' => 'IRBK1178','qty' => 1 ],
        [ 'code' => 'IRBK1177','qty' => 1 ]];

echo implode('|', array_map(function ($val) {
  return $val['code'].':'.$val['qty'];
}, $arr));

<强>输出: -

IRBK1179:1|IRBK1178:1|IRBK1177:1

答案 3 :(得分:1)

Simple solution using array_reduce function:

// $arr is the initial array
$result = array_reduce($arr, function($a, $b){
   $next = $b['code'].":".$b['qty'];
   return (!$a)? $next : (((is_array($a))? $a['code'].":".$a['qty'] : $a)."|".$next);
});

print_r($result);

The output:

IRBK1179:1|IRBK1178:1|IRBK1177:1

答案 4 :(得分:0)

这是我的版本:

<?php

$arr = [
    0 => ['code' => 'IRBK1179', 'qty' => 1],
    1 => ['code' => 'IRBK1178', 'qty' => 1],
    2 => ['code' => 'IRBK1177', 'qty' => 1],
];

$str = implode("|", array_map(function ($value) {return implode(":", array_values($value));}, array_values($arr)));

var_dump($str); # "IRBK1179:1|IRBK1178:1|IRBK1177:1"

答案 5 :(得分:0)

$array = array ( 
                '0' => array ( 
                    'code' => 'IRBK1179', 
                    'qty' => '1' 
                ), 
                '1' => array ( 
                    'code' => 'IRBK1178', 
                    'qty' => '1' 
                ), 
                '2' => array ( 
                    'code' => 'IRBK1177', 
                    'qty' => '1' 
                ) 
);

$string ='';
foreach($array as $key=>$value){

    $string .= $value['code'].':'.$value['qty'].'|';

}
echo $string;