Search code examples
phpcodeigniter-2

how can i write pyramid logic in php for given pattern?


The pattern is given below And i need pyramid logic for given pattern.

pyramid logic


Solution

  • A simple for loop with some basic logic should suffice.

    <?php
    
    $a = [6,8,6,7,3,5,5,4,1,1,9,5,1,5,6];
    
    $x = 0;
    for($i=0;$i<=count($a);$i++) {
       if(in_array($i, [5,9,12,14])) { //if we need a break point
          $x++; //increment the break counter
          echo PHP_EOL . str_repeat(' ', $x); //new line plus indentation
       }
       echo $a[$i] .' '; //echo the number and spacing
    }
    

    https://eval.in/538507