Reputation: 2706
I have an array variable $arr = array('A','B','C','D');
$number = 5; (This is dynamic)
My new array value should be
$arr = array('A','B','C','D','E','F','G','H','I');
If
$number = 3;
Output should be:
$arr = array('A','B','C','D','E','F','G');
If $number
variable will come more than 22
then print array from A
to Z
and with AA, AB, AC..
etc.
How to do that in PHP code?
Upvotes: 0
Views: 941
Reputation: 1642
You can increment letters by incrementing it, then store it that array itself. This will print two letter sequence also ie., AA, AB ...
$arr = array('A','B','C','D');
$item = end($arr) ;
$i = 0 ;
while( $i++ < $number ) {
$arr[] = ++$item ;
}
print_r($arr) ;
Upvotes: 1
Reputation: 85
$output = array();
$arr = array(A,B,C,D,E);
$data = range('F','Z');
$num = 4;
if($num < 22){
$output = array_merge($arr,array_slice($data, 0, $num));
}else{
// Write ur format here..
// $output = array('AA','AB',......,'AZ');
}
echo '<pre>';
print_r($output);
Upvotes: 0
Reputation: 810
How about this one: https://3v4l.org/IGhoL
<?php
/**
* Increments letter
* @param int $number
* @param array &$arr
*/
function increment($number, &$arr) {
$char = end($arr);
$char++;
for ($i = 0; $i < $number; $i++, $char++) {
$arr[] = $char;
}
}
$arr = range('A', 'D');
$number = 30;
increment($number, $arr);
var_dump($arr);
Upvotes: 2
Reputation: 126
Here is example to add chars from alphabet to array with offset. This works for one char. If you wish to work for more chars use loop in loop.
for ($c = ord('A') + $offset ;$c <= ord('Z');$c++) {
Array[] += chr($c);
}
Upvotes: 0