Php problem with outputting letters

How can I get alphabet like this?

So I got something like this:

$alphabet = ‘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’;
$alphabetArr = explode(‘,’, $alphabet);

for ($i = 0; $i < count($alphabetArr; $i++) {
for ($k = 0; k < count($alphabetArr; $k++) {
echo $alphabetArr[$k];
}
// break line;
}

I can’t get letter b in second row as first character, and letter a as last character in second row? I would appreciate any help

I am a great believer in keeping things simple, which I admit does mean my code is sometimes longer and less intuitive than others.

For me this is a simple matter of 4 strings.
1 - Take your first string $one = ‘abcdefghijklmnopqrstuvwxyz’
2 - use substr() to create 2 new strings based on the first string so $two = the end right hand character and $three = the left 25 characters
3 - then add the 2 strings together so $four = $two.$three
4 - set $one = $4 and loop

So basically a loop that runs 26 times, splitting the string in two parts consisting of 25 and 1 character, add them together and do it again

I got bored … here ya go…

<?php
//alphabet split
$one='abcdefghijklmnopqrstuvwxyz';

$x = 1;

while($x <= 26) {
	echo $one.'<br>';
  $two=substr($one,-1);
  $three=substr($one,0,25);
  $one=$two.$three;
  
  
  $x++;
} 
1 Like

Sorry ! just checked and realised I was doing it the wrong way. I was taking last letter and putting it at the beginning. You wanted to take first letter and put it at the end so I amended the code

<?php
//alphabet split
$one='abcdefghijklmnopqrstuvwxyz';

$x = 1;

while($x <= 26) {
  echo $one.'<br>';
  $two=substr($one,0,1);
  $three=substr($one,1,25);
  $one=$three.$two;
  
  
  $x++;
} 

Though to modify the poster’s original code (so that they can see they were on roughly the right track idea-wise), you could have done:

$alphabet = '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';
$alphabetArr = explode(',', $alphabet);

for ($i = 0; $i < count($alphabetArr); $i++) {
 for ($k = $i; $k < $i + count($alphabetArr); $k++) {
  echo $alphabetArr[$k % count($alphabetArr)];
 }
 echo "<br>";
}
1 Like

Yeah more applicable to his code, I agree. Thing is to be honest, I have always had a personal phobia of arrays, don’t know why, I just fear them, black magic voodoo - I am the same with preg - I need to embrace them I guess and stop avoiding them :blush:

Wasn’t a criticism, for the record, just trying to point out to the OP that they weren’t far from the mark to begin with :wink:

avoiding arrays is difficult, and they are very powerful. preg is a tool that gets overused/abused when simpler methods can do the job.

Never took it as a criticism my friend - it was a good point and I deliberately avoided the array solution because I don’t understand them. But I am going to take your code as a learning tool because now I have two different solutions to a known problem and I can cross reference and maybe start using arr, arrra, arrra, arrays - There I said it! :grinning:

2 Likes

You didn’t though, you just don’t know you’re using them :wink:

(my own interpretation of substr-under-the-hood)

function substr($string, $start, $length = PHP_INT_MAX) {
  if(!is_string($string)) { throw TypeMismatch; }
  if(!is_int($start)) { throw TypeMismatch; }
  if(!is_int($length)) { throw TypeMismatch; }

  $string = explode($string); //Hey look an array.

  //Sanitize inputs.
  if(count($string) < $start) { return false; }
  elseif($start < 0) { $start = count($string)+$start; }
  
  if($length === 0 || $length === FALSE || is_null($length)) { return ""; }
  elseif(($start + $length) > count($string)) { $end = count($string); }
  elseif($length < 0) { $end = count($string) + $length; }
  else { $end = $start + $length }

  //Compile output
  $output = "";
  for($i = $start; $i < $end; $i++) {
    $output .= $string[$i];
  }
  return $output;
}

doooh !

A string is also ‘an array of characters’ for the purposes of indexing - $one[2] == ‘c’.

We’ll open your eyes slowly, but there’s arrays all around you, if you but look for them :wink:

Well, I guess it’s time… get ready for some array posts !!! :grin:

Slight modification to @m_hutley’s example. By the way, what is this for?

$alphabetArr = range('a', 'z');

for ($i = 0; $i < count($alphabetArr); $i++) {
 for ($k = $i; $k < $i + count($alphabetArr); $k++) {
  echo $alphabetArr[$k % count($alphabetArr)];
 }
 echo "<br>";
}

In case you want a hard to read solution -


for($x=0,$alpha=range('a','z');$x<count($alpha);print implode($alpha).'<br>',$x++,$alpha[]=array_shift($alpha));
1 Like

With simple formatting, still a much cleaner solution.

Since the length of the $alpha array is a known constant, you could tighten up the code even more and eliminate the count function call.

for ($x = 0, $alpha = range('a', 'z');
    $x < 26;
    print implode($alpha) . '<br>', $x++, $alpha[] = array_shift($alpha)
);
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.