Sorting a multi-dimensional array in it's entirety

I have a multi dimensional array with various fields of data in it. I need to sort the array by a specific field on the second level of the array and keep all other fields relational. Here's an example of the code:

while ($postdata = mysql_fetch_array($firstresult)) {

    $postarray['structure']['count'] = $postarray['structure']['count'] + 1;
    $postarray['structure']['overview'] .= "\n" . $postdata['pin'];
    $postarray['structure'][$i] = $postdata['pin'];

    $postarray[$postdata[pin]]['username'] = $postdata['username'];
    $postarray[$postdata[pin]]['email'] = $postdata['email'];
    $postarray[$postdata[pin]]['secondemail'] = $postdata['secondemail'];
    $postarray[$postdata[pin]]['emaildisp'] = $postdata['emaildisp'];
    $postarray[$postdata[pin]]['date'] = $postdata['date'];
    $postarray[$postdata[pin]]['subject'] = $postdata['subject'];
    $postarray[$postdata[pin]]['body'] = $postdata['body'];

I hope that displays correctly. Here's an example of how it's shown in the browser:

for ($i = 1; $i <= $totalposts; $i++) {

    $currentpin = $postarray[structure][$i];

    echo $postarray[$currentpin]['subject'];
    echo $postarray[$currentpin]['username'];
    echo $postarray[$currentpin]['date'];

I hope that gives an idea of what I'm doing. What I want to do is have a function to sort it by a particular field, such as subject, or username, etc.

However for the life of me I can't figure out how to do this with this particular array.

Who is Participating?
SynstheConnect With a Mentor Commented:
Well instead of sorting the array itself, why not create an additional array to use as a reference point, and work on that?

Figure out what values you need from the original array, then go through and grab their index/id/whatever. Pipe each one into another array, and then sort that array. Then looping through that array use it's values to get what you need from the original array.

phpaddictAuthor Commented:
Thanks, I didn't think of that. I was trying to work entirely on the array itself without using any outside variables, which limited me I guess.

Much easier than writing a user defined function for usort!
All Courses

From novice to tech pro — start learning today.