This question already has answers here:
PHP array combinations
(8 answers)
PHP algorithm to generate all combinations of a specific size from a single set
(5 answers)
Closed 5 years ago.
Its hard to explain this in the title
I have array of different value. lets say this is the array:
$char = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");
how do I print every possible 3 char of this array.
like start off...
aaa
aab
aac
and so on...
Here is the example of possible two alphanumerics
<?php
$start = base_convert("10",36,10);
$end = base_convert("zz",36,10);
for($start;$start <= $end;$start++){
echo base_convert($start,10,36)."\n";
}
?>
Live Demo
You can start from 100 to zzz to get all possible digits of three albhanumeric.
You need to loop 3's to find your desire output.
$char = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");
$length = count($char);
for($i = 0; $i < $length; $i++)
for($j = 0; $j < $length; $j++)
for($k = 0; $k < $length; $k++)
echo $char[$i].$char[$j].$char[$k]."<br/>";
Output:
aaa
aab
aac
aad
aae
aaf
aag
aah
aai
...
Related
This question already has answers here:
Break long string into pieces php
(5 answers)
Closed 3 years ago.
I want to make a program which splits a long number into pieces of 13 digited number such that I can loop for every 13 digits just using php.
$number = 012345678901230123456789123
Should output
0123456789123
0123456789123
And it should be for any large number having the number of digit multiple of 13.It looks about looping and algorithm but I want to make it as short as possible and I have doubts on how to do it. So I am just asking about the main concept.
The most dynamic solution is probably to use array_functions on the string.
So str_split to make it array then chunk it in size 13 and implode the arrays.
$number = "012345678901230123456789123";
$arr = array_chunk(str_split($number), 13);
foreach($arr as &$val){
$val = implode($val);
}
https://3v4l.org/LsNFt
You can create a function where you can use your string and size as parameter and return an array of strings of the desired length:
function splitString($str, $packetSize) {
$output = [];
$size = strlen($str);
for ($i = 0; $i < $size; $i += $packetSize) {
if ($i + $packetSize < $size) {
$output[]= substr($str, $i, $packetSize);
} else {
$output[]=substr($str, $i);
}
}
return $output;
}
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 5 years ago.
I'm trying to make a for loop with numbers given by user ($start and $limit), but I want to be able to write '0001' as $start and '1000' as $limit and print each one of numbers. The problem is, only the first number is printed as '000..' and after increment, those zeros disappear. This is my code:
$start = 0001;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
echo $i.'<br>';
}
outputs:
001
2
3
...
1000
Is there any way to make it as:
0001
0002
...
1000
Try
$start = 1;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
printf("%04d<br>",$i);
}
This question already has answers here:
PHP random string generator
(68 answers)
Closed 7 years ago.
I want to generate a 6 character long unique key in php in which first 3 should be alphabets and next 3 should be digits.
I know about the uniqid() function but it generates 13 characters long key and also it wont fit in my requirement as I need first 3 characters as alphabets and next 3 as numbers.
Any way in which I can modify uniqid() to fit in my requirements?
I also dont want any collisions because if that happens my whole database will be wasted that is why I can't use rand function because it is very likely that I will get collisions
You could create a manual randomizer like this:
<?php
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$numbers = '0123456789';
$value = '';
for ($i = 0; $i < 3; $i++) {
$value .= substr($alphabet, rand(0, strlen($alphabet) - 1), 1);
}
for ($i = 0; $i < 3; $i++) {
$value .= substr($numbers, rand(0, strlen($numbers) - 1), 1);
}
The $value variable will then be a string like "axy813" or "nbm449".
<?php
$alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$numbers = "1234567890";
$randstr = '';
for($i=0; $i < 6; $i++){
if($i<3){
$randstr .= $alphabets[rand(0, strlen($alphabets) - 1)];
} else {
$randstr .= $numbers[rand(0, strlen($numbers) - 1)];
}
}
echo $randstr;
?>
this will do the work for you
This question already has answers here:
PHP - Sequence through array and repeat [modulo-operator]
(3 answers)
Closed 9 days ago.
I have an array with 3 entries like that:
0 => "Banana",
1 => "Apple",
2 => "Strawberry"
Now, when using a for-loop like:
for($i = 1; $i < $foo; $i++) {
$myarray[$i] = $fruitarray[$i];
}
And $i gets higher than 2, we run out of this $fruitarray. So what I want to do now is always to start at the beginning of the array when it's over. So that 3 outputs "Banana", 7 "Apple" etc.
What is the best practice to achieve this (especially if $fruitarray contains many entries)?
This is generally accomplished using the modulo operator.
for($i = 1; $i < $foo; $i++) {
$myarray[$i] = $fruitarray[$i % count($fruitarray)];
}
http://php.net/manual/en/language.operators.arithmetic.php
for($i = 1; $i < $foo; $i++) {
$myarray[$i] = $fruitarray[$i%3];
}
Notice the $i%3, this is pronounced "i mod 3" and it means divide i by 3, and return the leftovers. So, 4%3=1, 7%3=1, 11%3=2, etc.
Just make sure $foo is more than 3, and this should wrap around. If $fruitarray is arbitrarily bigger than 3 items, use
$i%count($fruitarray)
One possible solution would be to check for when $i is equal to $foo - 1 and reset it to 1:
for($i = 1; $i < $foo; $i++) {
$myarray[$i] = $fruitarray[$i];
if($i == ($foo - 1))
$i = 1;
}
You should to use the modulus operator (Modulus remainder of $a divided by $b)
for($i = 1; $i < $foo; $i++) {
$j=$i%count($fruitarray);
$myarray[$i] = $fruitarray[$j];
}
http://php.net/manual/en/language.operators.arithmetic.php
Doing this with eager code is essentially intractable, as it would cause an infinite loop:
$i = 0;
$numFruits = count($fruitarray);
while (true) { // infinite loop!
$myarray[$++i] = $fruitarray[$i % $numFruits];
}
But you can come up with working code if, instead of an array, you use a function to fetch the value:
function getFruitAt($index) use ($fruitarray) {
$numFruits = count($fruitarray);
$realIndex = $index % $numFruits;
return $fruitarray[$realIndex];
}
Or, if you want to get fancy, you can use a generator or define your own Iterator type.
This question already has answers here:
make string of N characters
(5 answers)
Closed 2 years ago.
I'm creating quite a complex html <table> layout and at this early stage it quite time consuming for me to copy and paste each <tr> in order to generate dummy content.
My idea was to specify a dummy <tr> as a $var and then output that x number of times using a function as below:
$html = "<tr>//content</tr>";
function dummy_html($html, $times){
$i = 0;
for ($i <= $times) {
echo $html;
$i = $i++;
}
}
echo dummy_html($html, 5);
But this is returning a parse error on the for line any idea why that might be ?
PHP has a function already
echo str_repeat($html,5);
Your for loop is incorrect. It should be something like:
for( $i = 0; $i <= $times; $i++ ) {
echo $html;
}
Update
#Your Common Sense's solution is better: str_repeat (http://php.net/manual/en/function.str-repeat.php)
http://php.net/manual/en/control-structures.for.php
for should use the notation: for (set arguments, conditions, command to run at the end of the loop), therefor should be:
for($i = 0; $i <= $times; $i++)
Also, I would recommend using str_repeat (http://php.net/manual/en/function.str-repeat.php)