Ahmed
Ahmed

Reputation: 1533

PHP get letters between two letters

I have two PHP dynamic variables that returns two letters based on a SQL query and for this example I will use a two random fixed letters:

$firstletter = "F";
$lastletter = "A";

I need to return all the letters in between first and last letter as an array so I can loop through them. For this example it should return F,E,D,C,B,A

Any ideas how i can do this?

Upvotes: 4

Views: 637

Answers (3)

Rajeev
Rajeev

Reputation: 1

You need all the alphabets between two given letters. you can use range to get that. e.g. $letters = range('f', 'a');

Upvotes: 0

HPierce
HPierce

Reputation: 7409

range() will do this:

<?php

var_dump(range('F', 'A'));
array(6) {
  [0]=>
  string(1) "F"
  [1]=>
  string(1) "E"
  [2]=>
  string(1) "D"
  [3]=>
  string(1) "C"
  [4]=>
  string(1) "B"
  [5]=>
  string(1) "A"
}

Upvotes: 2

prakash tank
prakash tank

Reputation: 1267

Try this :

$array = range('A', 'F');
print_r($array);

for reverse values :

$array = range('A', 'F');
print_r(array_reverse($array)); 

Upvotes: 1

Related Questions