PHP - Filter an array string alphabetically





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-4















I have an array of strings



$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];


I want to retrieve all the strings that starts with letter A, then B, then C, and so on for each alphabet letters.



What is the best way to do it avoing useless code repetitions?










share|improve this question




















  • 9





    have you tried doing it yourself?

    – Yousaf
    Nov 22 '18 at 12:09











  • Question, do you have it?

    – u_mulder
    Nov 22 '18 at 12:13








  • 1





    Use array_filter()

    – Magnus Eriksson
    Nov 22 '18 at 12:17


















-4















I have an array of strings



$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];


I want to retrieve all the strings that starts with letter A, then B, then C, and so on for each alphabet letters.



What is the best way to do it avoing useless code repetitions?










share|improve this question




















  • 9





    have you tried doing it yourself?

    – Yousaf
    Nov 22 '18 at 12:09











  • Question, do you have it?

    – u_mulder
    Nov 22 '18 at 12:13








  • 1





    Use array_filter()

    – Magnus Eriksson
    Nov 22 '18 at 12:17














-4












-4








-4


0






I have an array of strings



$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];


I want to retrieve all the strings that starts with letter A, then B, then C, and so on for each alphabet letters.



What is the best way to do it avoing useless code repetitions?










share|improve this question
















I have an array of strings



$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];


I want to retrieve all the strings that starts with letter A, then B, then C, and so on for each alphabet letters.



What is the best way to do it avoing useless code repetitions?







php arrays filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 16:12







albertovalerio

















asked Nov 22 '18 at 12:07









albertovalerioalbertovalerio

42




42








  • 9





    have you tried doing it yourself?

    – Yousaf
    Nov 22 '18 at 12:09











  • Question, do you have it?

    – u_mulder
    Nov 22 '18 at 12:13








  • 1





    Use array_filter()

    – Magnus Eriksson
    Nov 22 '18 at 12:17














  • 9





    have you tried doing it yourself?

    – Yousaf
    Nov 22 '18 at 12:09











  • Question, do you have it?

    – u_mulder
    Nov 22 '18 at 12:13








  • 1





    Use array_filter()

    – Magnus Eriksson
    Nov 22 '18 at 12:17








9




9





have you tried doing it yourself?

– Yousaf
Nov 22 '18 at 12:09





have you tried doing it yourself?

– Yousaf
Nov 22 '18 at 12:09













Question, do you have it?

– u_mulder
Nov 22 '18 at 12:13







Question, do you have it?

– u_mulder
Nov 22 '18 at 12:13






1




1





Use array_filter()

– Magnus Eriksson
Nov 22 '18 at 12:17





Use array_filter()

– Magnus Eriksson
Nov 22 '18 at 12:17












5 Answers
5






active

oldest

votes


















2














You can create a temporary multidimensional array with first letter as index. Try -



$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

foreach($cities as $city) {
$first = substr($city, 0, 1);
$temp_cities[$first] = $city;
}

var_dump($temp_cities);


Output



array(8) {
["N"]=>
array(1) {
[0]=>
string(8) "New York"
}
["B"]=>
array(1) {
[0]=>
string(6) "Boston"
}
["L"]=>
array(1) {
[0]=>
string(11) "Los Angeles"
}
["C"]=>
array(2) {
[0]=>
string(10) "Cincinnati"
[1]=>
string(7) "Chicago"
}
["H"]=>
array(1) {
[0]=>
string(7) "Houston"
}
["P"]=>
array(1) {
[0]=>
string(12) "Philadelphia"
}
["D"]=>
array(1) {
[0]=>
string(6) "Dallas"
}
["S"]=>
array(1) {
[0]=>
string(7) "Seattle"
}
}


To find cities start with 'C' do - var_dump($temp_cities['C'])






share|improve this answer
























  • Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

    – albertovalerio
    Nov 23 '18 at 15:55



















1














Please try this



<?php

$cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];
$new_array = ;
$search_string = 'C'; // Letter to search
foreach ($cities as $city) {
if (strpos($city, $search_string) === 0) {
$new_array = $city;
}
}
print_r($new_array);// New array will contian list of identified cities
?>





share|improve this answer































    0














    usort($cities, function($a, $b){
    return $a <=> $b;
    });





    share|improve this answer



















    • 4





      Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

      – iainn
      Nov 22 '18 at 12:15



















    0














    If I understand correctly, you're trying to create 26 arrays, one for each letter of the alphabet, each array containing cities starting with that letter.



    <?php
    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    //Our 26 arrays
    $arrA = array();
    $arrB = array();
    $arrC = array();
    $arrD = array();
    $arrE = array();
    $arrF = array();
    $arrG = array();
    $arrH = array();
    $arrI = array();
    $arrJ = array();
    $arrK = array();
    $arrL = array();
    $arrM = array();
    $arrN = array();
    $arrO = array();
    $arrP = array();
    $arrQ = array();
    $arrR = array();
    $arrS = array();
    $arrT = array();
    $arrU = array();
    $arrV = array();
    $arrW = array();
    $arrX = array();
    $arrY = array();
    $arrZ = array();

    //Fill the array for that city.
    foreach ($cities as $city){
    $key = substr($city, 0, 1);
    ${"arr".$key} = $city;
    }
    //Let's just test this worked.
    var_dump($arrC);
    var_dump($arrB);
    var_dump($arrN);

    ?>





    share|improve this answer



















    • 5





      Using multidimensional array is better than 26 separate variable

      – Mohammad
      Nov 22 '18 at 12:53













    • @Mohammad I agree that would be better, but it's not what the OP asked for.

      – Ben Hillier
      Nov 26 '18 at 9:41



















    0














    You can simply sort array with



    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    sort($cities);





    share|improve this answer


























    • Please post documentation links to the actual manual rather than a third party site

      – Nick
      Nov 24 '18 at 6:39












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430676%2fphp-filter-an-array-string-alphabetically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can create a temporary multidimensional array with first letter as index. Try -



    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    foreach($cities as $city) {
    $first = substr($city, 0, 1);
    $temp_cities[$first] = $city;
    }

    var_dump($temp_cities);


    Output



    array(8) {
    ["N"]=>
    array(1) {
    [0]=>
    string(8) "New York"
    }
    ["B"]=>
    array(1) {
    [0]=>
    string(6) "Boston"
    }
    ["L"]=>
    array(1) {
    [0]=>
    string(11) "Los Angeles"
    }
    ["C"]=>
    array(2) {
    [0]=>
    string(10) "Cincinnati"
    [1]=>
    string(7) "Chicago"
    }
    ["H"]=>
    array(1) {
    [0]=>
    string(7) "Houston"
    }
    ["P"]=>
    array(1) {
    [0]=>
    string(12) "Philadelphia"
    }
    ["D"]=>
    array(1) {
    [0]=>
    string(6) "Dallas"
    }
    ["S"]=>
    array(1) {
    [0]=>
    string(7) "Seattle"
    }
    }


    To find cities start with 'C' do - var_dump($temp_cities['C'])






    share|improve this answer
























    • Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

      – albertovalerio
      Nov 23 '18 at 15:55
















    2














    You can create a temporary multidimensional array with first letter as index. Try -



    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    foreach($cities as $city) {
    $first = substr($city, 0, 1);
    $temp_cities[$first] = $city;
    }

    var_dump($temp_cities);


    Output



    array(8) {
    ["N"]=>
    array(1) {
    [0]=>
    string(8) "New York"
    }
    ["B"]=>
    array(1) {
    [0]=>
    string(6) "Boston"
    }
    ["L"]=>
    array(1) {
    [0]=>
    string(11) "Los Angeles"
    }
    ["C"]=>
    array(2) {
    [0]=>
    string(10) "Cincinnati"
    [1]=>
    string(7) "Chicago"
    }
    ["H"]=>
    array(1) {
    [0]=>
    string(7) "Houston"
    }
    ["P"]=>
    array(1) {
    [0]=>
    string(12) "Philadelphia"
    }
    ["D"]=>
    array(1) {
    [0]=>
    string(6) "Dallas"
    }
    ["S"]=>
    array(1) {
    [0]=>
    string(7) "Seattle"
    }
    }


    To find cities start with 'C' do - var_dump($temp_cities['C'])






    share|improve this answer
























    • Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

      – albertovalerio
      Nov 23 '18 at 15:55














    2












    2








    2







    You can create a temporary multidimensional array with first letter as index. Try -



    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    foreach($cities as $city) {
    $first = substr($city, 0, 1);
    $temp_cities[$first] = $city;
    }

    var_dump($temp_cities);


    Output



    array(8) {
    ["N"]=>
    array(1) {
    [0]=>
    string(8) "New York"
    }
    ["B"]=>
    array(1) {
    [0]=>
    string(6) "Boston"
    }
    ["L"]=>
    array(1) {
    [0]=>
    string(11) "Los Angeles"
    }
    ["C"]=>
    array(2) {
    [0]=>
    string(10) "Cincinnati"
    [1]=>
    string(7) "Chicago"
    }
    ["H"]=>
    array(1) {
    [0]=>
    string(7) "Houston"
    }
    ["P"]=>
    array(1) {
    [0]=>
    string(12) "Philadelphia"
    }
    ["D"]=>
    array(1) {
    [0]=>
    string(6) "Dallas"
    }
    ["S"]=>
    array(1) {
    [0]=>
    string(7) "Seattle"
    }
    }


    To find cities start with 'C' do - var_dump($temp_cities['C'])






    share|improve this answer













    You can create a temporary multidimensional array with first letter as index. Try -



    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

    foreach($cities as $city) {
    $first = substr($city, 0, 1);
    $temp_cities[$first] = $city;
    }

    var_dump($temp_cities);


    Output



    array(8) {
    ["N"]=>
    array(1) {
    [0]=>
    string(8) "New York"
    }
    ["B"]=>
    array(1) {
    [0]=>
    string(6) "Boston"
    }
    ["L"]=>
    array(1) {
    [0]=>
    string(11) "Los Angeles"
    }
    ["C"]=>
    array(2) {
    [0]=>
    string(10) "Cincinnati"
    [1]=>
    string(7) "Chicago"
    }
    ["H"]=>
    array(1) {
    [0]=>
    string(7) "Houston"
    }
    ["P"]=>
    array(1) {
    [0]=>
    string(12) "Philadelphia"
    }
    ["D"]=>
    array(1) {
    [0]=>
    string(6) "Dallas"
    }
    ["S"]=>
    array(1) {
    [0]=>
    string(7) "Seattle"
    }
    }


    To find cities start with 'C' do - var_dump($temp_cities['C'])







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 12:32









    Sougata BoseSougata Bose

    26.9k53165




    26.9k53165













    • Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

      – albertovalerio
      Nov 23 '18 at 15:55



















    • Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

      – albertovalerio
      Nov 23 '18 at 15:55

















    Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

    – albertovalerio
    Nov 23 '18 at 15:55





    Thanks that solves my question and seems to me the best way to do it. My point was to avoid useless code repetitions. Thanks again.

    – albertovalerio
    Nov 23 '18 at 15:55













    1














    Please try this



    <?php

    $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];
    $new_array = ;
    $search_string = 'C'; // Letter to search
    foreach ($cities as $city) {
    if (strpos($city, $search_string) === 0) {
    $new_array = $city;
    }
    }
    print_r($new_array);// New array will contian list of identified cities
    ?>





    share|improve this answer




























      1














      Please try this



      <?php

      $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];
      $new_array = ;
      $search_string = 'C'; // Letter to search
      foreach ($cities as $city) {
      if (strpos($city, $search_string) === 0) {
      $new_array = $city;
      }
      }
      print_r($new_array);// New array will contian list of identified cities
      ?>





      share|improve this answer


























        1












        1








        1







        Please try this



        <?php

        $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];
        $new_array = ;
        $search_string = 'C'; // Letter to search
        foreach ($cities as $city) {
        if (strpos($city, $search_string) === 0) {
        $new_array = $city;
        }
        }
        print_r($new_array);// New array will contian list of identified cities
        ?>





        share|improve this answer













        Please try this



        <?php

        $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];
        $new_array = ;
        $search_string = 'C'; // Letter to search
        foreach ($cities as $city) {
        if (strpos($city, $search_string) === 0) {
        $new_array = $city;
        }
        }
        print_r($new_array);// New array will contian list of identified cities
        ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 12:20









        VamsiVamsi

        285217




        285217























            0














            usort($cities, function($a, $b){
            return $a <=> $b;
            });





            share|improve this answer



















            • 4





              Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

              – iainn
              Nov 22 '18 at 12:15
















            0














            usort($cities, function($a, $b){
            return $a <=> $b;
            });





            share|improve this answer



















            • 4





              Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

              – iainn
              Nov 22 '18 at 12:15














            0












            0








            0







            usort($cities, function($a, $b){
            return $a <=> $b;
            });





            share|improve this answer













            usort($cities, function($a, $b){
            return $a <=> $b;
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 12:14









            TsVTsV

            45417




            45417








            • 4





              Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

              – iainn
              Nov 22 '18 at 12:15














            • 4





              Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

              – iainn
              Nov 22 '18 at 12:15








            4




            4





            Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

            – iainn
            Nov 22 '18 at 12:15





            Or, y'know, just sort($cities). But either way sorting isn't what the question is about.

            – iainn
            Nov 22 '18 at 12:15











            0














            If I understand correctly, you're trying to create 26 arrays, one for each letter of the alphabet, each array containing cities starting with that letter.



            <?php
            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            //Our 26 arrays
            $arrA = array();
            $arrB = array();
            $arrC = array();
            $arrD = array();
            $arrE = array();
            $arrF = array();
            $arrG = array();
            $arrH = array();
            $arrI = array();
            $arrJ = array();
            $arrK = array();
            $arrL = array();
            $arrM = array();
            $arrN = array();
            $arrO = array();
            $arrP = array();
            $arrQ = array();
            $arrR = array();
            $arrS = array();
            $arrT = array();
            $arrU = array();
            $arrV = array();
            $arrW = array();
            $arrX = array();
            $arrY = array();
            $arrZ = array();

            //Fill the array for that city.
            foreach ($cities as $city){
            $key = substr($city, 0, 1);
            ${"arr".$key} = $city;
            }
            //Let's just test this worked.
            var_dump($arrC);
            var_dump($arrB);
            var_dump($arrN);

            ?>





            share|improve this answer



















            • 5





              Using multidimensional array is better than 26 separate variable

              – Mohammad
              Nov 22 '18 at 12:53













            • @Mohammad I agree that would be better, but it's not what the OP asked for.

              – Ben Hillier
              Nov 26 '18 at 9:41
















            0














            If I understand correctly, you're trying to create 26 arrays, one for each letter of the alphabet, each array containing cities starting with that letter.



            <?php
            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            //Our 26 arrays
            $arrA = array();
            $arrB = array();
            $arrC = array();
            $arrD = array();
            $arrE = array();
            $arrF = array();
            $arrG = array();
            $arrH = array();
            $arrI = array();
            $arrJ = array();
            $arrK = array();
            $arrL = array();
            $arrM = array();
            $arrN = array();
            $arrO = array();
            $arrP = array();
            $arrQ = array();
            $arrR = array();
            $arrS = array();
            $arrT = array();
            $arrU = array();
            $arrV = array();
            $arrW = array();
            $arrX = array();
            $arrY = array();
            $arrZ = array();

            //Fill the array for that city.
            foreach ($cities as $city){
            $key = substr($city, 0, 1);
            ${"arr".$key} = $city;
            }
            //Let's just test this worked.
            var_dump($arrC);
            var_dump($arrB);
            var_dump($arrN);

            ?>





            share|improve this answer



















            • 5





              Using multidimensional array is better than 26 separate variable

              – Mohammad
              Nov 22 '18 at 12:53













            • @Mohammad I agree that would be better, but it's not what the OP asked for.

              – Ben Hillier
              Nov 26 '18 at 9:41














            0












            0








            0







            If I understand correctly, you're trying to create 26 arrays, one for each letter of the alphabet, each array containing cities starting with that letter.



            <?php
            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            //Our 26 arrays
            $arrA = array();
            $arrB = array();
            $arrC = array();
            $arrD = array();
            $arrE = array();
            $arrF = array();
            $arrG = array();
            $arrH = array();
            $arrI = array();
            $arrJ = array();
            $arrK = array();
            $arrL = array();
            $arrM = array();
            $arrN = array();
            $arrO = array();
            $arrP = array();
            $arrQ = array();
            $arrR = array();
            $arrS = array();
            $arrT = array();
            $arrU = array();
            $arrV = array();
            $arrW = array();
            $arrX = array();
            $arrY = array();
            $arrZ = array();

            //Fill the array for that city.
            foreach ($cities as $city){
            $key = substr($city, 0, 1);
            ${"arr".$key} = $city;
            }
            //Let's just test this worked.
            var_dump($arrC);
            var_dump($arrB);
            var_dump($arrN);

            ?>





            share|improve this answer













            If I understand correctly, you're trying to create 26 arrays, one for each letter of the alphabet, each array containing cities starting with that letter.



            <?php
            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            //Our 26 arrays
            $arrA = array();
            $arrB = array();
            $arrC = array();
            $arrD = array();
            $arrE = array();
            $arrF = array();
            $arrG = array();
            $arrH = array();
            $arrI = array();
            $arrJ = array();
            $arrK = array();
            $arrL = array();
            $arrM = array();
            $arrN = array();
            $arrO = array();
            $arrP = array();
            $arrQ = array();
            $arrR = array();
            $arrS = array();
            $arrT = array();
            $arrU = array();
            $arrV = array();
            $arrW = array();
            $arrX = array();
            $arrY = array();
            $arrZ = array();

            //Fill the array for that city.
            foreach ($cities as $city){
            $key = substr($city, 0, 1);
            ${"arr".$key} = $city;
            }
            //Let's just test this worked.
            var_dump($arrC);
            var_dump($arrB);
            var_dump($arrN);

            ?>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 12:38









            Ben HillierBen Hillier

            1,7501611




            1,7501611








            • 5





              Using multidimensional array is better than 26 separate variable

              – Mohammad
              Nov 22 '18 at 12:53













            • @Mohammad I agree that would be better, but it's not what the OP asked for.

              – Ben Hillier
              Nov 26 '18 at 9:41














            • 5





              Using multidimensional array is better than 26 separate variable

              – Mohammad
              Nov 22 '18 at 12:53













            • @Mohammad I agree that would be better, but it's not what the OP asked for.

              – Ben Hillier
              Nov 26 '18 at 9:41








            5




            5





            Using multidimensional array is better than 26 separate variable

            – Mohammad
            Nov 22 '18 at 12:53







            Using multidimensional array is better than 26 separate variable

            – Mohammad
            Nov 22 '18 at 12:53















            @Mohammad I agree that would be better, but it's not what the OP asked for.

            – Ben Hillier
            Nov 26 '18 at 9:41





            @Mohammad I agree that would be better, but it's not what the OP asked for.

            – Ben Hillier
            Nov 26 '18 at 9:41











            0














            You can simply sort array with



            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            sort($cities);





            share|improve this answer


























            • Please post documentation links to the actual manual rather than a third party site

              – Nick
              Nov 24 '18 at 6:39
















            0














            You can simply sort array with



            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            sort($cities);





            share|improve this answer


























            • Please post documentation links to the actual manual rather than a third party site

              – Nick
              Nov 24 '18 at 6:39














            0












            0








            0







            You can simply sort array with



            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            sort($cities);





            share|improve this answer















            You can simply sort array with



            $cities = ['New York', 'Boston', 'Los Angeles', 'Cincinnati', 'Chicago', 'Houston', 'Philadelphia', 'Dallas', 'Seattle'];

            sort($cities);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 5:31

























            answered Nov 22 '18 at 12:29









            NickNick

            53




            53













            • Please post documentation links to the actual manual rather than a third party site

              – Nick
              Nov 24 '18 at 6:39



















            • Please post documentation links to the actual manual rather than a third party site

              – Nick
              Nov 24 '18 at 6:39

















            Please post documentation links to the actual manual rather than a third party site

            – Nick
            Nov 24 '18 at 6:39





            Please post documentation links to the actual manual rather than a third party site

            – Nick
            Nov 24 '18 at 6:39


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430676%2fphp-filter-an-array-string-alphabetically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?