I wanted to create a for loop that would make a pattern like this.
* two tree four five
one * tree four five
one two * four five
one two tree * five
one two tree four *
* * tree four five
one * * four five
one two * * five
one two tree * *
* * * four five
one * * * five
one two * * *
* * * * five
one * * * *
* * * * *
Instead I end up finding this How to get these patern using for loop
The guys are using JavaScript:
const temp = ["one","two","three","four","five"];
let arrayResult = []
for(let i = 0; i< temp.length; i++){
for(let j = 0; j< temp.length - i; j++){
let tempArr = temp.slice();
let stars = Array(i+1).fill('*')
tempArr.splice(j, i+1, ...stars);
arrayResult.push(tempArr);
}}
I wanted to pull this of using php but I can't seem to get it work even with array_splice()
$temp = array("one","two","three","four","five");
$arrayResult = array();
for($i = 0; $i< count($temp); $i++){
for($j = 0; $j< count($temp) - $i; $j++){
$tempArr = $temp.slice();
$stars = Array($i+1).fill('*')
$tempArr.splice($j, $i+1, ...$stars);
$arrayResult [] = $tempArr;
}}