wchamber22 Posted February 19, 2012 Share Posted February 19, 2012 Hi freaks, Got a simple one for ya, I THINK? I have a multi-array that resembles this.. $_SESSION["book_array"] = array(0 => array("plantID" => $plantID, "botanicalName" => $botanicalName, "commonName" => $commonName, "use" => $use)); I would like to sort it by the botanicalName key of the inner array before I call the forech loop that renders the display so that after it renders the table the items will be seen alphabetically, code below.. <?php $bookOutput = ""; //$plant_use_array = ''; if (!isset($_SESSION["book_array"]) || count($_SESSION["book_array"]) < 1) { $bookOutput = '<tr><td colspan="4"><h6>Your Book is EMPTY!</h6></td></tr>'; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["book_array"] as $each_item) { $plantID = $each_item['plantID']; $botanicalName = $each_item['botanicalName']; $botanicalName = stripslashes($botanicalName); $commonName = $each_item['commonName']; $commonName = stripslashes($commonName); $use = $each_item['use']; //$x = $i + 1; // Dynamic table row assembly $bookOutput .= "<tr>"; $bookOutput .= '<td><a href="plant_details.php?plantID=' . $plantID . '" id="bodyLink">' . $botanicalName . '</a></td>'; $bookOutput .= '<td>' . $commonName . '</td>'; $bookOutput .= '<td><strong>' . $use . '</strong></td>'; $bookOutput .= '<td><form action="book.php" method="post"><input name="removeBtn" type="submit" value="Remove"/><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $bookOutput .= '</tr>'; $i++; } } ?> Any help would be much appreciated! Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/ Share on other sites More sharing options...
kicken Posted February 19, 2012 Share Posted February 19, 2012 Create a comparison function and use usort. The function will take two arguments, which will be elements of the main array. Access each arguments 'botanicalName' key and compare them and return the result (as either -1, 0, or 1) Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/#findComment-1318846 Share on other sites More sharing options...
wchamber22 Posted February 19, 2012 Author Share Posted February 19, 2012 Thanks Kicken, I believe I am on the right track after exploring the usort function. Below is what I have come up with thus far, but am still not getting the alphabetical ordered results as I wish. $bookArray = $_SESSION["book_array"]; function botanicalCmp ($a, $b) { if ($a['botanicalName'] == $b['botanicalName']) return 0; if ($a['botanicalName'] < $b['botanicalName']) return -1; return 1; } usort($bookArray, 'botanicalCmp'); Here is my entire output code, with a little more help I know I can get this, thanks in advance. <?php $bookOutput = ""; //$plant_use_array = ''; if (!isset($_SESSION["book_array"]) || count($_SESSION["book_array"]) < 1) { $bookOutput = '<tr><td colspan="4"><h6>Your Book is EMPTY!</h6></td></tr>'; } else { $bookArray = $_SESSION["book_array"]; function botanicalCmp ($a, $b) { if ($a['botanicalName'] == $b['botanicalName']) return 0; if ($a['botanicalName'] < $b['botanicalName']) return -1; return 1; } usort($bookArray, 'botanicalCmp'); // Start the For Each loop $i = 0; foreach ($_SESSION["book_array"] as $each_item) { $plantID = $each_item['plantID']; $botanicalName = $each_item['botanicalName']; $botanicalName = stripslashes($botanicalName); $commonName = $each_item['commonName']; $commonName = stripslashes($commonName); $use = $each_item['use']; //$x = $i + 1; // Dynamic table row assembly $bookOutput .= "<tr>"; $bookOutput .= '<td><a href="plant_details.php?plantID=' . $plantID . '" id="bodyLink">' . $botanicalName . '</a></td>'; $bookOutput .= '<td>' . $commonName . '</td>'; $bookOutput .= '<td><strong>' . $use . '</strong></td>'; $bookOutput .= '<td><form action="book.php" method="post"><input name="removeBtn" type="submit" value="Remove"/><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $bookOutput .= '</tr>'; $i++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/#findComment-1318945 Share on other sites More sharing options...
wchamber22 Posted February 20, 2012 Author Share Posted February 20, 2012 Can anyone help me with this, I have been working all day with no success. I am too new to php grasp this concept, sorry. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/#findComment-1319043 Share on other sites More sharing options...
kicken Posted February 20, 2012 Share Posted February 20, 2012 usort($bookArray, 'botanicalCmp'); foreach ($_SESSION["book_array"] as $each_item) { You're sorting the variable $bookArray, but your looping over $_SESSION['book_array']. You need to sort the same variable you loop over. Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/#findComment-1319047 Share on other sites More sharing options...
wchamber22 Posted February 20, 2012 Author Share Posted February 20, 2012 Yeah Kicken that was it, now I feel really dumb. Anyway thanks again. Kicken >= THE MAN! Quote Link to comment https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/#findComment-1319051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.