/    Sign up×
Community /Pin to ProfileBookmark

Sorting an array by element in PHP

Hi there,

I’ll get right to it. Let’s say my data file contains the following:

[code=php]
—START DATA FILE—
|~~|20060228225218|~~|February 28, 2006 – 10:52 PM|~~|Fred|~~|Smith|~~|123 Abc Street|~~|Banora Point|~~|NSW|~~|4211|~~|
|~~|20060228225309|~~|February 28, 2006 – 10:53 PM|~~|Sally|~~|Citizen|~~|456 Def Street|~~|Nerang|~~|QLD|~~|2486|~~|
|~~|20060228225531|~~|February 28, 2006 – 10:55 PM|~~|Joe|~~|Bloe|~~|555 Evergreen Tce|~~|Springfield|~~|California|~~|12345|~~|
—END DATA FILE—
[/code]

So far I have managed to get my script to write the elements from each row into a nice neat list using the code below (unecessary bits taken out). What I would like to be able to do is sort my entries alphabetically by $row[3] (the fourth element in each record). Can someone show me how to do this? I have tried many variations but with no luck! Any help is very much appreciated!

Regards,

Chris

[code=php]
$record = file($data_file);
rsort($record);
$jmlrec = count($record);

$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
$no = $page*$max_entry_per_page-$max_entry_per_page;
// This is the default message when there are no records posted – Chris
if ($jmlrec == 0) echo “<TR><TD colspan=3 bgcolor=’#FFE1E1′ align=’center’><FONT SIZE=’3′ FACE=’$font_face’>There are no address book entries listed at this present time.</FONT></TD></TR>”;

$w = 0; //– Alternating color values
for ($i=0; $i<$max_entry_per_page; $i++) {
$no++;
$recno = $no-1;
if (isset($record[$recno])) {
$row = explode(“|~~|”,$record[$recno]);

echo “<p>Here’s where I insert elements from my array:<br>
Element1: $row[0]<br>
Element2: $row[1]<br>
Element3: $row[2]<br>
Element4: $row[3]<br>
Element5: $row[4]<br>
etc, etc , etc</p>”;
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423Mar 01.2006 — an easy way is using exec (i prefer)..

store all the elements in third row in a variable ..

exec("echo $var | sort",$sortedArray,$res);

foreach($sortedArray as ....

trying doing this way..
Copy linkTweet thisAlerts:
@Chris_JacksauthorMar 03.2006 — Thanks sridhar_423. Unfortunately that went a little over my head. I had a crack at it but couldn't get that to work. I'm not sure whether I'm using "foreach" or "exec" incorrectly. Probably "exec" because I've never used that before (and had a little trouble grasping the concept, even after looking at it on PHP.net. Maybe I'm just too tired tonight!).

Can you show me what you mean by example? Or otherwise, any other suggestions? Once again, any help is very much appreciated! Thanks mate!

Cheers,

Chris
Copy linkTweet thisAlerts:
@NogDogMar 03.2006 — OK, here's what I came up with:
[code=php]
$lines = file($datafile) or die("Failed to read data file");
foreach($lines as $line)
{
if(strpos($line, '|~~|') !== FALSE)
{
$thisArray = explode('|~~|', $line);
array_pop($thisArray); // get rid of empty element at end
array_shift($thisArray); // get rid of empty element at start
$data[] = $thisArray;
}
}
foreach($data as $key => $array)
{
$lname[$key] = $array[3];
}
array_multisort($lname, SORT_ASC, $data);
// show results:
echo "<pre>"; print_r($data); echo "</pre>n";
[/code]
×

Success!

Help @Chris_Jacks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...