Jump to content

best way to sort?


vito_huang

Recommended Posts

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!

 

 

 

 

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

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.