vito_huang Posted April 19, 2007 Share Posted April 19, 2007 i got a table with all the names in mysql, and i have use pear's DB to get all the names out i want to sort all the names start with A in array_a, names start with B in array_b, ETC i just wondering what is the best way to sort my problem. N.B don't want to use 26 sql to get all the names. or using 25 if statement to seperate them. i am open minded and happen to know other people's idea thanks in advance! Quote Link to comment Share on other sites More sharing options...
mpharo Posted April 19, 2007 Share Posted April 19, 2007 Best way is probably the GROUP BY clause in your SELECT statement, you would just do SELECT * FROM table GROUP BY field ASC or something along that lines... Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 19, 2007 Share Posted April 19, 2007 You could adapt this foreach to your row looping ... <?php $testNames = array('joe', 'bob', 'jan', 'carl'); $splitArray = array(); foreach($testNames as $name) { $firstLetter = strtolower(substr($name, 0, 1)); // If the letter hasn't been used before create an empty array for the letter if(!isset($splitArray['letter_' . $firstLetter])) { $lettery['letter_' . $firstLetter] = array(); } $letter['letter_' . $firstLetter][] = $name; } ?> with that <?php print_r($letter); ?> Outputs... Array ( [letter_j] => Array ( [0] => joe [1] => jan ) [letter_b] => Array ( [0] => bob ) [letter_c] => Array ( [0] => carl ) ) or you could do <?php extract($letter); print_r($letter_j); print_r($letter_b); print_r($letter_c); ?> Outputs... Array ( [0] => joe [1] => jan ) Array ( [0] => bob ) Array ( [0] => carl ) Quote Link to comment Share on other sites More sharing options...
vito_huang Posted April 19, 2007 Author Share Posted April 19, 2007 thanks for this good idea Quote Link to comment Share on other sites More sharing options...
vito_huang Posted April 19, 2007 Author Share Posted April 19, 2007 in the your code what does the following code does? $splitArray = array(); // If the letter hasn't been used before create an empty array for the letter if(!isset($splitArray['letter_' . $firstLetter])) { $lettery['letter_' . $firstLetter] = array(); } this is what i think since $splitArray is empty array at the begin so $lettery['letter_' . $firstLetter] = array(); will got execute everytime. and $letter['letter_'.$firstLetter][] = $name will automatically create. is my thinking right? i have delete those line and the code still works sorry to bored you, but i am just want to know. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 19, 2007 Share Posted April 19, 2007 no, sorry... I was in a rush when I wrote the code.... <?php $testNames = array('joe', 'bob', 'jan', 'carl'); $letters = array(); foreach($testNames as $name) { $firstLetter = strtolower(substr($name, 0, 1)); // If the letter hasn't been used before create an empty array for the letter if(!isset($letters['letter_' . $firstLetter])) { $letters['letter_' . $firstLetter] = array(); } $letters['letter_' . $firstLetter][] = $name; } ?> this should be correct, sorry about that. I had in the if() statement because I believe it might flag an undefined variable error (Under E_NOTICE) if I were to just do $letters['letter_' . $firstLetter][] without declaring $letters['letter_' . $firstLetter] an array first. I could be mistaken in this, but it's just a habit of mine to declare a variable an array before adding to it. Quote Link to comment Share on other sites More sharing options...
vito_huang Posted April 20, 2007 Author Share Posted April 20, 2007 thanks, that's pretty good solution, cos before i was thinking to use recursive loop ( like binary search stuff ), but you solution is brilliant. 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.