Sort by alphabetical order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Aha! Good catch, Info. Try this:

Code: Select all

<?php


$lines = file('c:\dwagent\clients.dwa');
$delimiter = "]";

function sortByValue($value1, $value2) {
    echo strcmp($value1[1], $value2[1]);
}

foreach ($lines as $line_num => $line) {

    $parameters = array();

    $splitcontents = explode($delimiter, $line);
    $counter = 0;

    foreach ( $splitcontents as $color ) {

        $counter++;
        if ($counter == 1){
            $parameters[] = array('Name', $color);
        } elseif ($counter == 3){
            $parameters[] = array('SQL', $color);
        } elseif ($counter == 5){
            $parameters[] = array('WEB', $color);
        }
    }

    usort($parameters, 'sortByValue');

    foreach ($parameters as $parameter) {
        echo '<b>' . $parameter[0] . ': </b> ' . $parameter[1] . ' <br />';
    }
}


?>
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Ok that took out the big space, lol. I don't see any extra lines in the file at the top.

I can't post the file but here is sort of what it looks like

Client1]\\path]server]db]host]service5
Client2]\\path2]server2]db2]host4]service
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sort by alphabetical order

Post by VladSun »

Code: Select all

$parameters = array();

if (($handle = fopen("c:\dwagent\clients.dwa", "r")) !== FALSE) 
{
	while (($csv = fgetcsv($handle, 1000, "]")) !== FALSE) 
	{
		$parameters[] = array
		(
			'Name' 	=> $csv[1],
			'SQL'	=> $csv[3],
			'WEB'	=> $csv[5],
		);
	}
}

function sortByName($value1, $value2) 
{
	return strcmp($value1['Name'], $value2['Name']);
}

usort($parameters, 'sortByName');

foreach ($parameters as $parameter) 
{
	echo $parameter['Name'] . ': ' . $parameter['SQL'] . ' : ' . $parameter['WEB'];
}
PS:

Code: Select all

echo strcmp($value1[1], $value2[1]);
:lol:
There are 10 types of people in this world, those who understand binary and those who don't
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Vlad! It worked! Thanks, and thanks everyone for your help on this. A round on me :drunk:
Post Reply