1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php sorting array via alphabetical order

Discussion in 'PHP' started by ironmankho, Dec 20, 2011.

  1. #1
    i have 50 names of movies in txt file that i will get via file gat content....

    1-now my first target it will be unique
    2- Second i want to shown in alphabetical way like
    i want to show data of 'A', 'B'

    please tell me how it is possible and logic used ....and want do it only
    php not using mysql....



    A Good Old Fashioned Orgy
    Mr. Bean's Holiday
    The Inbetweeners Movie
    Man on the Train
    We Need to Talk About Kevin
    Hugo
    The Ides of March
    The Muppets
    Don't Let Him In
    Mercenaries
    Straw Dogs
    Underground
    Red Dog
    Seeking Justice
    Restitution
    Rampart
    Loosies
    Jack and Jill
    Footloose
    The River Why
    Albert Nobbs
    Martha Marcy May Marlene
    Alyce
    On the Inside
    50/50
    The Thing
    The Debt
    A Very Harold & Kumar 3D Christmas
    The Twilight Saga: Breaking Dawn - Part 1
    One Day
    Puncture
    The Family Tree
    Tyrannosaur
    Born Bad
    Forged
    Carnage
    Like Crazy
    Our Idiot Brother
    Carjacked
    Moneyball
    Immortals
    Tower Heist
    Secrets in the Walls
    Texas Killing Fields
    Venus & Vegas
    11-11-11
    A Dangerous Method
    The Devil's Double
    Another Earth
    Black Butterflies
     
    Solved! View solution.
    ironmankho, Dec 20, 2011 IP
  2. luckyguy354

    luckyguy354 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to sort array data, just use function sort() or natsort()
     
    luckyguy354, Dec 20, 2011 IP
  3. #3
    PHP has native functions for both tasks, therefore you really don't need any logic to accomplish what you want to do:

    <?php
    $contents = file_get_contents('movies.txt');					// Get the file contents
    $originalArray = array_map('trim', explode("\n", $contents));	// Separate the values to into an array
    $finalArray = array_unique($originalArray);						// Remove duplicated entries
    sort($finalArray, SORT_STRING);									// Sort the array
    $contents = implode("\n", $finalArray);							// Turn the new array back into a string
    file_put_contents('movies.txt', $contents);						// Write it to the file
    PHP:
     
    proactiv3, Dec 21, 2011 IP
  4. ironmankho

    ironmankho Active Member

    Messages:
    393
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Thanks proactiv3

    it is possible to show only data of start with alphabet C
     
    ironmankho, Dec 21, 2011 IP
  5. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #5
    
    $movies = array("Carnage", "Like Crazy", "Our Idiot Brother", "Carjacked", "Moneyball");
    foreach ($movies as &$value) {
        $first_letter = substr($value, 0, 1);
        if (strtoupper($first_letter == "C")) {
            echo $value."<br />";
        }
    }
    
    PHP:
     
    dujmovicv, Dec 21, 2011 IP