Jump to content

[SOLVED] noob: need help sorting a multi-dimensional array alphabetically descending


Recommended Posts

I have an array that contains movie information and am trying to figure out how to sort it via PHP but need help.

 

Here is a sample of the array:

(
    [0] => Array
        (
            [id] => 1
            [title] => A.I. Artificial Intelligence
            [rating] => PG13
            [description] => Sometime in the distant future, after the polar icecaps have melted....
        )

    [1] => Array
        (
            [id] => 2
            [title] => Shawshank Redemption
            [rating] => R
            [description] => Andy Dufresne, a mild mannered New England banker, is convicted of murdering his wife....
        )

    [2] => Array
        (
            [id] => 3
            [title] => Pulp Fiction
            [rating] => R
            [description] => An inside look at a memorable community of criminals.... 
        )
)

 

I know how to ORDER BY in MySQL, but how would I sort this array in descending alphabetical order so that when displayed in an html select drop down box it is ordered by title?

 

E.G.:

A.I. Artificial Intelligence

Pulp Fiction

Shawshank Redemption

yes, i've tried several varients but none seem to work how I need them to.

 

arsort() sorts by ID, not by title

array_multisort($title_array, SORT_DESC) sorts again by ID not by title like I need it to

ksort($title_array, SORT_STRING); also does not give the required results

 

It seems like it should be simple, but i'm going bald pulling out my hair over this thing lol.

Isnt there a way to sort an array by one of it's values? something like:

sigkill_sort($title_array, 'title');

 

Or do I have to make a function to do this? If so, how would I do that???

 

heres my select code. I attempted a ksort but it doesnt work. Doesnt order the titles alphabetically descending like I need:

ksort($title_array['title']);
foreach($title_array as $value) {
$id = $value['id'];
$title = $value['title'];
$t = '<option value="' . $id . '"';

if ($value['id'] === $_POST['id'])
	$t .= ' SELECTED';
echo $t . '>'. $title . '</option>\n';	
}

 

$title_array contains the above mentioned array.

Thanks for the link. I read it and tried to apply it to my array, but it still doesnt seem to be sorting by title in the drop down list like I need it to.

 

Theres go to be a way to do this. I'm new to PHP and dont know a whole lot, but i'm sure theres a way. Doesnt anyone know how to do this?

<?php
function scmp($a, $b, $key="title") {
    return strcmp($a[$key], $b[$key]);
}

$fruits[0]["title"] = "ZShould be last";
$fruits[1]["title"] = "AA Should be first";
$fruits[2]["title"] = "AB Should be second";

usort($fruits, "scmp");

while (list($key, $value) = each($fruits)) {
    echo "\$fruits[$key]: " . $value["title"] . "<br />";
}

die();
?>

 

Never mind the name of the array ($fruits).  That should get you what you want.

Thanks premiso. I adapted your example to my application and it works, it sorts the list, finally :)

 

I have a question though, while waiting for a response to this post I searched elsewhere for a solution and I found another code that also works.

 

The question is, which of these two codes would be better to use?

 

Your code:

function cmp($a, $b, $key="title") {
    return strcmp($a[$key], $b[$key]);
}

usort($title_array, "cmp");

while (list($key, $value) = each($title_array)) {
    echo "\$title_array[$key]: " . $value["title"] . "<br />";
}

die();

 

Or the other code I found:

function cmp($a, $b) {
return strcmp($a["title"], $b["title"]);
}

usort($title_array, "cmp");

foreach($title_array as $value)
{
   echo $value['title']."<br>";
}

echo "<\pre>";

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.