Rifts Posted October 8, 2014 Share Posted October 8, 2014 Hey guys, I have an array of names sorted alphabetically. I'm trying to loop through them and create new arrays keyed by the letters of the alphabet containing the names which start with that letter. so in the end i'm trying to create something like this: $newarray = array( 'a' => array('anna','alan','andrew'), 'b' => array('bert','ben','bob'), 'c' => array('cait','carry'), etc,etc ); thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2014 Share Posted October 8, 2014 try $names = array ('anna','bert','ben','cait','alan','andrew','bob','carry'); sort($names); foreach ($names as $n) { $newArray[$n[0]][] = $n; } Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) You sir are a god among men. Thank you. now what if in the names array there were some non-alpha values like numbers. How could I get all those into an array with the key '#' ? Edited October 8, 2014 by Rifts Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2014 Share Posted October 8, 2014 do you mean $nums = array (1234,28975,9258,44,950,40582,21,456); sort($nums); foreach ($nums as $n) { $x = "$n"; // cast the number as a string $newArray[$x[0]][] = $n; } Gives Array ( [2] => Array ( [0] => 21 [1] => 28975 ) [4] => Array ( [0] => 44 [1] => 456 [2] => 40582 ) [9] => Array ( [0] => 950 [1] => 9258 ) [1] => Array ( [0] => 1234 ) ) Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) No sorry for not explaining better. I meant $names = array ('anna','bert','ben','cait','alan','andrew','bob','carry', '23 kenny','10','43 lala'); I was going to try something like this foreach ($names as $n) { if( is_numeric($n)) { $newArray[$n[0]]['#'] = $n; } else { $newArray[$n[0]][] = $n; } } Edited October 8, 2014 by Rifts Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted October 8, 2014 Solution Share Posted October 8, 2014 OK, plan B $names = array ('anna','bert','ben','cait','alan','andrew','bob','carry', '23 kenny','10','43 lala'); sort($names); foreach ($names as $n) { $x = "$n"; // cast the name as a string if (is_numeric($x[0])) $newArray['#'][] = $n; else $newArray[$x[0]][] = $n; } Gives Array ( [#] => Array ( [0] => 10 [1] => 23 kenny [2] => 43 lala ) [a] => Array ( [0] => alan [1] => andrew [2] => anna ) [b] => Array ( [0] => ben [1] => bert [2] => bob ) [c] => Array ( [0] => cait [1] => carry ) ) Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted October 9, 2014 Share Posted October 9, 2014 You may want to use natsort() on the array if want the natural order for numbers. If are grouping 0-9 by first characters sort() is fine Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.