Outputting array contents as nested list in PHP

后端 未结 5 1781
谎友^
谎友^ 2021-01-20 08:49

I have the array array ( [0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10)) and I would like to display it like this:

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 09:25

    Here is another way to do this (demo here):

     elements per 
  • $aElements = 2; $totalElems = count($tab); //open the list echo "
    • "; for($i=0;$i<$totalElems;$i++){ if($i != 0 && ($i%$aElements) == 0){ //check if I'm in the nTh element. echo "
    • "; //if so, close curr
    • and open another
    • element } //print elem inside the
    • echo "".$tab[$i].""; } //close the list echo "
    "; ?>
  • Tip explanation: $i%n (mod) equals 0 when $i is the nTh element (remainder of division is 0)

    EDITED: made a general solution

提交回复
热议问题