Jump to content

range() - mixing letters with numbers?


deansatch

Recommended Posts

Can I somehow mix letters with numbers to create an array with range()?

 

e.g. I want my array for postcode ranges such as array('AB1','AB2', 'AB3', 'AB4', 'AB5', 'Ab6', 'AB7', 'AB8', 'IV5', 'IV6', 'IV7', 'IV8', 'IV9', 'IV10');

 

I know I can do range(1,8); but was wondering if there was something along the lines of range("AB1", "AB8");

Link to comment
Share on other sites

You could do it, i suppose.  The first thing that sprung in my mind was a load of nested loops.  Which would confuse the hell out of me.

 

I agree with Thorpe.  For the sake of errors: if you're wanting postcodes, then get a database CSV and drop that into the database.  they typically come with long&lat codes making it easier to get distances etc.

Link to comment
Share on other sites

Would be simple enough to loop through and build a range with a prefix:

 

function prefix_range($prefix, $from, $to)
{
    if (!is_numeric($from) || !is_numeric($to) || $from > $to) {
        return false;
    }

    $range = array();

    for ($i = $from; $i <= $to; $i++) {
        array_push($range, $prefix . $i);
    }

    return $range;
}

 

Example usage:

 

$ab_range = prefix_range('AB', 1, ;

/*Array
(
    [0] => AB1
    [1] => AB2
    [2] => AB3
    [3] => AB4
    [4] => AB5
    [5] => AB6
    [6] => AB7
    [7] => AB8
)*/

 

Edit

 

Not sure how this will help with real postcodes though?

Link to comment
Share on other sites

Thanks all. I eventually did it like so...

 

 

function mixArrayKeyValue(&$value,$key){$value=$value.$key;}
$arrayAB = array_fill(17, 4,'AB');
array_walk($arrayAB, 'mixArrayKeyValue');

$arrayAB2 = array_fill(26, 4,'AB');
array_walk($arrayAB2, 'mixArrayKeyValue');

$mergedArray = array_merge($arrayAB, $arrayAB2);
//etc....

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.