3v4l.org

run code in 300+ PHP versions simultaneously
<?php $txt = "3000,MELBOURNE 3001,MELBOURNE 3002,EAST MELBOURNE 3003,WEST MELBOURNE 3603,WEST SYDNEY 3103,NORTH PERTH"; $rows = explode(PHP_EOL, $txt); $arr= []; foreach($rows as $row){ List($postcode, $suburb) = explode(",", $row); If(in_array(substr($suburb,0,4), array("EAST", "WEST")) || in_array(substr($suburb,0,5), array("SOUTH", "NORTH"))){ $temp = explode(" ", $suburb); $arr[$temp[1]][$temp[0]][] = $postcode; }else{ $arr[$suburb][] = $postcode; } } Var_dump($arr);

preferences:
16.33 ms | 402 KiB | 5 Q