We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41395
    • 8 Posts
    Hi:

    Could someone please see if they are having the same issue? I have an array in a snippet and when I tried to use either asort() or sort() which are both standard PHP functions, they didn't work.

    ex.

    $myarray = array();
    $myarray = ("bob","jim","terry");
    $result = asort($myarray);
    


    The output was 0. Interesting to note that array_unique() worked fine.



      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The sort and asort functions do not return the sorted array, they sort the actual array. They return a boolean value for success or failure.

      It's returning 0 because you are loading the array incorrectly. Try this:
      <?php
      $myarray = array("jim", "terry", "bob");
      asort($myarray);
      print_r($myarray);


      http://php.net/manual/en/language.types.array.php
      An existing array can be modified by explicitly setting values in it.

      This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).
      [ed. note: sottwell last edited this post 13 years, 11 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 41395
        • 8 Posts
        Quote from: sottwell at Nov 17, 2012, 12:37 AM
        The sort and asort functions do not return the sorted array, they sort the actual array. They return a boolean value for success or failure.

        It's returning 0 because you are loading the array incorrectly. Try this:
        <!--?php
        $myarray = array("jim", "terry", "bob");
        asort($myarray);
        print_r($myarray);


        http://php.net/manual/en/language.types.array.php
        An existing array can be modified by explicitly setting values in it.

        This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).
        -->

        Thank you! My bad. Works great now.