Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/257294-sort-a-multidimensional-array-by-key/
Share on other sites

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)

 

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++; 
    } 
}
?>

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.