Using this function,prt_pattern("341","0")
, to print out
0
00
00
000
I'm stuck at converting "341"
into integer and store it each number the following was the result i wanted
array[0] = 3
array[1] = 4
array[2] = 1
The flow I'm going to plan for this function is, convert string into int and store it by character, and then find the highest value. After that using 2 for loop
, 1st for loop is loop from largest number to 1 and 2nd loop is for looping the number if 4 = 4
, print 0
else print " "
and value at array[1] -= 1
.
Hope anyone can help me with this.
This should work. First I create an array of each number inside the string (cast to integer), then find the highest number from it using the max
function.
<?php
$array = (function ($str): array {
$a = [];
$strlen = strlen($str);
for($i = 0; $i < $strlen; $i++) {
$a[] = (int) $str[$i];
}
return $a;
})("3429");
var_dump($array);
/**
array(4) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(2)
[3]=>
int(9)
}
**/
var_dump(max($array)); //int(9)
Test it online here : http://sandbox.onlinephpfunctions.com/code/2e3446d4475c3d69215857ed33a2c1f94f628e0b
edit
As Barmar
pointed out in the comments, its way easier to use PHPs function str_split
like so:
$array = str_split("3429");
Which will create an array just like previous mentioned.
(I feel silly for not thinking of that function.)