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
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:
$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: