<?php
$postcodes = ['SO30 0NG', 'SO30 OTD', 'SO31 1TG', 'RG51 5HG', 'RG51 7GH'];
$result= array();
Foreach ($postcodes as $postcode){
$short =Substr($postcode,0,4);
If(!isset($result[$short])) $result[$short]=[];
$result[$short][]= $short;
}
Var_dump($result);
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- array(3) {
["SO30"]=>
array(2) {
[0]=>
string(4) "SO30"
[1]=>
string(4) "SO30"
}
["SO31"]=>
array(1) {
[0]=>
string(4) "SO31"
}
["RG51"]=>
array(2) {
[0]=>
string(4) "RG51"
[1]=>
string(4) "RG51"
}
}
preferences:
151.22 ms | 409 KiB | 5 Q