Jump to content
ffej2ffej

sorting by an element within an array within an array

Recommended Posts

I have an array full of smaller arrays. The smaller arrays contain elements, one of which is a date. (Not a timestamp, a date such as 1, 2, 3, etc.) I want to sort the small arrays within the large array in order of this date. I don't see anything in the docs for PHP on how to do this.Let me state this another way. I have an array called $eventsThisMonth. I would like to sort $eventsThisMonth by $eventsThisMonth[every][dateOfThisOccurrance]. Thank you in advance for your great help and advice. Jeff P.S. I tried sorting "manually" using an outside loop that goes 1-31 (for the possible values of the date) and an inner loop that goes 0-count($eventsThisMonth) and compares the dateOfThisOccurrance of each smaller array to the index of the outside loop. If equal, it does an array_push to another array. This still didn't work; the other array was in the same order as the original.

Share this post


Link to post
Share on other sites

Ok, here is my attempt to "manually" sort. The code follows. for($x=1;$x<32;$x++){for($y=0;$y<count($thisMonthArray);$y++){if($thisMonthArray[$y][dateOfThisOccurance]==$x){array_push($thisMonthArraySorted,$thisMonthArray[$y]);echo "<script>alert('x='.$x.'y='.$y)</script>";}//closes if()}//closes inner loop (all elements in $thisMonthArray)}//closes outer loop (1 - 31) echo "<table border=3><tr><td>Event Name</td><td>Organization</td><td>Event Date</td><td>Event Description</td></tr>";for($i=0;$i < count($thisMonthArraySorted);$i++){echo "<tr><td>".$thisMonthArraySorted[$i][eventName]."</td><td>".$thisMonthArraySorted[$i][nameAddressPhone]."</td><td>".$thisMonthArraySorted[$i][dateOfThisOccurance]."</td><td>".$thisMonthArraySorted[$i][description]."</td></tr>";}//closes for()echo "</table>";

Share this post


Link to post
Share on other sites

I fixed it. I realized the problem was not with the sorting mechanism, but the fact that I was sorting a date (01, 02, etc.) to a string of the entire date. I extracted the substring then compared it and it works fine. I did not use any of the sorting functions built in to PHP, but rather used my own algorithm. The way I'm imagining the site in full use, it won't really matter because there won't ever be any more than a few dozen (at worst) records to sort.Thank you all for your suggestions and help.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...