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:

<PRE>
while ($postdata = mysql_fetch_array($firstresult)) {

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

    $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'];
}
</PRE>

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

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

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

    echo $postarray[$currentpin]['subject'];
    echo $postarray[$currentpin]['username'];
    echo $postarray[$currentpin]['date'];
}
</PRE>

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.

phpaddictAsked:
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.


0
 
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!
0
All Courses

From novice to tech pro — start learning today.