-2

I need to implode an multi-dimensional array in a string using implode, i tried using the array_map shown here: stackoverflow.com but i failed.

Array:

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

Desired Output:

IRBK1179:1|IRBK1178:1|IRBK1177:1
Community
  • 1
  • 1

6 Answers6

3

Use foreach and implode() inner array with : and then implode() new array with |. Try below code.

$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);

Output

IRBK1179:1|IRBK1178:1|IRBK1177:1

Working Online Demo: Click Here

Use explode() to get back array from string.

Try below code.

$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);

Output

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

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

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

)

Working Demo : Click Here

RJParikh
  • 4,096
  • 1
  • 19
  • 36
3

As i commented out, use the implode and foreach. for inner array use : and for outer array use |.

$str = array();
foreach($arr as $val){
    $str[] = implode(":", $val);
}
echo implode("|", $str); //IRBK1179:1|IRBK1178:1|IRBK1177:1
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
2

Both other answers given by Frayne and Ruchish are correct.

Here is another alternative using 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));

Output:-

IRBK1179:1|IRBK1178:1|IRBK1177:1
Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • Yes. It is. I have edited it as per your requirement and pointed out by Frayne Konok :-) – Ravi Hirani May 05 '16 at 08:46
  • Note that `implode()` does not look at the keys that are being imploded. So if by whatever effect the order of the inner keys `code` and `qty` are reversed, `implode()` will happily create a string with the wrong order. Bottom line: only use `implode()` on numeric arrays where the order of entries does not matter. – Sven May 06 '16 at 19:45
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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Here is my version:

<?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"
codepoet
  • 1
  • 2
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;
Vipin
  • 115
  • 1
  • 2
  • 13