convert 2d array to a string using php

前端 未结 5 1356
攒了一身酷
攒了一身酷 2021-01-22 06:39

I have the following array

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

I want to convert a string like the following:

         


        
5条回答
  •  醉梦人生
    2021-01-22 07:29

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

提交回复
热议问题