2

I have the following two-dimensional array:

01 03 02 15
05 04 06 10
07 09 08 11
12 14 13 16

I want to convert columns to rows then reduce the matrix to a string like the following:

01,05,07,12|03,04,09,14|02,06,08,13|15,10,11,16
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
smith
  • 5,341
  • 8
  • 31
  • 38

5 Answers5

7

I'm assuming that you have this array:

$array = array (
  array ('01','03','02','15'),
  array ('05','04','06','10'),
  array ('07','09','08','11'),
  array ('12','14','13','16')
);

In which case, you can do this:

$tmpArr = array();
foreach ($array as $sub) {
  $tmpArr[] = implode(',', $sub);
}
$result = implode('|', $tmpArr);
echo $result;

See it working

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
3
$input = array(
    array('01', '02', '03', '04'),
    array('11', '12', '13', '14'),
    array('21', '22', '23', '24'),
    array('31', '32', '33', '34'),
);
$newArray = array();
foreach($input as $rowIndex => $row) {
    foreach($row as $key => $val) {
        if(!$newArray[$key]) {
            $newArray[$key] = array();
        }
        $newArray[$key][$rowIndex] = $val;
    }
}
$strRows = array();
foreach($newArray as $key => $row) {
    $strRows[$key] = join(',', $row);
}
$output = join('|', $strRows);
Rudolph Gottesheim
  • 1,671
  • 1
  • 17
  • 30
1

The most elegant script for this "transpose and double-implode" task is to call array_map() with a spread operator on the input array, then join the columns with commas, the join the joined strings with pipes.

Code: (Demo)

var_export(
    implode(
        '|',
        array_map(
            fn() => implode(',', func_get_args()),
            ...$input
        )
    )
);

Or collect all of the column data in the function signature. (Demo)

var_export(
    implode(
        '|',
        array_map(
            fn(...$column) => implode(',', $column),
            ...$input
        )
    )
);

For similar questions that do not require a single string of output, but prefer an array of delimited strings, simply remove the outer implode() call. Namely:

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

Given your array as $myArray, like this:

$newArray = array();
foreach ($myArray as $row) {
    $rowValue = implode(',', $row);
    $newArray[] = $rowValue;
}
$finalString = implode('|', $newArray);
davidethell
  • 11,708
  • 6
  • 43
  • 63
0

Looking at the solutions proposed so far, only one actually generates the correct output requested by the OP.

So, stealing unashamedly from salathe's neat conversion, but with a transpose to provide the correct result:

$input = array ( 
    array ('01','03','02','15'), 
    array ('05','04','06','10'), 
    array ('07','09','08','11'), 
    array ('12','14','13','16'), 
); 

function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}

$transposed = transpose($input);

echo implode('|', array_map('implode', $transposed, array_fill(0, count($transposed), ','))); 
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • While this answer DOES transpose the array data; as of PHP8 its implementation of implode() causes a Fatal error. [Proof](https://3v4l.org/FSbQN) – mickmackusa Jul 25 '22 at 11:41