Transpose 2d array, join second level with commas, and join first level with pipes

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

Leave a Comment