Jump to content

sort($data);


Matt79

Recommended Posts

I have created a page that I place all of the acronyms that I come across.  I have them sorted in alphabetical order using the sort command. It sorts most of them correctly, but there are some that are in incorrect places. I have tried deleting the ones that are in the wrong place, but then new ones appear in their place. If anyone could give me some help that would be great. :-)

Below is the code that I created. I have all of the acronyms contained in the $raw_data variable.




$split=explode("1234567890",$raw_data);
sort($split);
$num=0;
$count=count($split);

while($num < $count)
{
echo $split[$num].'<br/>';
$num++;
}



Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/
Share on other sites

Ok you can see how it is being sorted here http://snyderit.info/test/acc.php

 

How I have the $raw_data setup is like this:

 

<?php
$raw_data="
customer premises equipment (CPE)1234567890
Service provider (SP)1234567890
virtual circuits (VC)1234567890
Point to Point Tunneling Protocol (PPTP)1234567890
Layer 2 Tunneling Protocol (L2TP)1234567890
IP security (IPsec)1234567890";


$split=explode("1234567890",$raw_data);
sort($split);
$num=0;
$count=count($split);

while($num < $count)
{
echo $split[$num].'<br/>';
$num++;
}
?>


 

Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998109
Share on other sites

It is included in my previous posts. I tried strtoupper() and it did not change anything.

I want it to be in alphabetical order on the webpage. You can see how it is coming out here:

http://snyderit.info/test/acc.php Please note the DES near the bottom of the page.

 

The script I am using is here:

<?php
$raw_data="
customer premises equipment (CPE)&
Service provider (SP)&
virtual circuits (VC)&
Point to Point Tunneling Protocol (PPTP)&
Layer 2 Tunneling Protocol (L2TP)&
IP security (IPsec)&";


$split=explode("&",$raw_data);
sort($split);
$num=0;
$count=count($split);

while($num < $count)
{
echo $split[$num].'<br/>';
$num++;
}
?>

 

I did remove some of the acronyms before I posted it on the form since there are 188 of them. But the number should not matter.

 

Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998207
Share on other sites

My 2cents

$raw_data="a customer premises equipment (CPE)
z Service provider (SP)
y virtual circuits (VC)
A Point to Point Tunneling Protocol (PPTP)
B Layer 2 Tunneling Protocol (L2TP)
6 IP security (IPsec)";

$split=explode("\n",$raw_data);
natcasesort($split);
foreach($split as $str){
echo "$str<br/>";
}

 

6 IP security (IPsec)

a customer premises equipment (CPE)

A Point to Point Tunneling Protocol (PPTP)

B Layer 2 Tunneling Protocol (L2TP)

y virtual circuits (VC)

z Service provider (SP)

Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998350
Share on other sites

It almost appears random.

Almost random, except for the extraneous space characters attached to the 'malfunctioning' values. Get rid of that whitespace and I'd hazard a guess that the problem will go away. :shy:

 

[ot]Why are you splitting on 1234567890 rather than something more usual like... a line break?[/ot]

Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998359
Share on other sites

Thanks MadTechie. I used the code you created and it worked, although I do not quite understand the differences. And I do not know why the a, z, y, A etc, are in your code. I am assuming it is to explain something but do not get it.

a customer premises equipment (CPE)
z Service provider (SP)
y virtual circuits (VC)
A Point to Point Tunneling Protocol (PPTP)
B Layer 2 Tunneling Protocol (L2TP)
6 IP security

 

 

My final script that works thanks to MadTechie:

 

 

<?php

$raw_data="
customer premises equipment (CPE)
Service provider (SP)
virtual circuits (VC)
Point to Point Tunneling Protocol (PPTP)
Layer 2 Tunneling Protocol (L2TP)
IP security (IPsec)
Provider Core (P)
";

$raw_data=strtoupper($raw_data);
$split=explode("\n",$raw_data);
natcasesort($split);
foreach($split as $str){



echo "$str<br/>";
}

?>

 

Thanks guys for all the help 8)

 

Link to comment
https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998423
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.