0

we need to list an array sorting by an object value.

We have a list of films, sorted by alphabetical order, but we want to sort by genres.

here is the code listing in alphabetical order.

<?php
    foreach ($films as $film_id => $film) {

    echo "<ul>";
    echo '<li id="film_thumb">';
    echo '<a href="watch.php?id=' . $film_id . '" alt="ID">';
    echo '<img class="thumb_res" src=" ' . $film["thumb"] . '" alt="' . $film["name"] . '">' ;
    echo '</a>';
    echo '</li>';
    echo "</ul>";
} ?>

the function listing in alphabetical order is in the array file

sort($films, SORT_FLAG_CASE);

Here is the array

$films = array ();
    $films[1] = array(
        "name" => "21 Jump Street",
        "year" => "2012",
        "genre" => "Commedia",
        "path" => "media/01.mp4",
        "thumb"=> "media/thumb/01.png",
        );

now, we need to create a page that list these items ( film thumb ) in categories, so if i click on genre "Action", the page must to show only selected film genre, and not the others.

Many thanks, Andrea

1
  • Use array_filter() to filter array by some value Commented Aug 7, 2014 at 14:07

1 Answer 1

1

You can sort your array by genre with usort() :

usort($films, function($a, $b){
    return strcasecmp($a['genre'], $b['genre']);
});

If you want to get films from the selected genre, you can use array_filter() :

$films = array_filter($films, function($film){
    return $film['genre'] == 'Commedia';
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.