Sort array without index

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christiang@lateral.net

    Sort array without index

    Hi guys I'd like to sort a multidimensiona l array that hasn't numerical
    index, in fact it is like:

    Array
    (
    [mobile] => Array
    (
    [name_str] => mobile
    [relationCount_i nt] => 1
    )

    [video] => Array
    (
    [name_str] => video
    [relationCount_i nt] => 2
    )

    [flash] => Array
    (
    [name_str] => flash
    [relationCount_i nt] => 1
    )

    [tool] => Array
    (
    [name_str] => tool
    [relationCount_i nt] => 1
    )

    )

    And I'd like to sort, for instance for the value of
    "relationCount_ int", so the new order should be something like:

    Array
    (
    [video] => Array
    (
    [name_str] => video
    [relationCount_i nt] => 2
    )
    [mobile] => Array
    (
    [name_str] => mobile
    [relationCount_i nt] => 1
    )


    [flash] => Array
    (
    [name_str] => flash
    [relationCount_i nt] => 1
    )

    [tool] => Array
    (
    [name_str] => tool
    [relationCount_i nt] => 1
    )

    )

    Any idea? I tried some script I found on php.net but didn't work. The
    could should run inside a class method.

    Thanks a lot, chr

  • Erwin Moller

    #2
    Re: Sort array without index

    christiang@late ral.net wrote:
    [color=blue]
    > Hi guys I'd like to sort a multidimensiona l array that hasn't numerical
    > index, in fact it is like:
    >
    > Array
    > (
    > [mobile] => Array
    > (
    > [name_str] => mobile
    > [relationCount_i nt] => 1
    > )
    >
    > [video] => Array
    > (
    > [name_str] => video
    > [relationCount_i nt] => 2
    > )
    >
    > [flash] => Array
    > (
    > [name_str] => flash
    > [relationCount_i nt] => 1
    > )
    >
    > [tool] => Array
    > (
    > [name_str] => tool
    > [relationCount_i nt] => 1
    > )
    >
    > )
    >
    > And I'd like to sort, for instance for the value of
    > "relationCount_ int", so the new order should be something like:
    >
    > Array
    > (
    > [video] => Array
    > (
    > [name_str] => video
    > [relationCount_i nt] => 2
    > )
    > [mobile] => Array
    > (
    > [name_str] => mobile
    > [relationCount_i nt] => 1
    > )
    >
    >
    > [flash] => Array
    > (
    > [name_str] => flash
    > [relationCount_i nt] => 1
    > )
    >
    > [tool] => Array
    > (
    > [name_str] => tool
    > [relationCount_i nt] => 1
    > )
    >
    > )
    >
    > Any idea? I tried some script I found on php.net but didn't work. The
    > could should run inside a class method.
    >
    > Thanks a lot, chr[/color]


    Hi

    Yes, it is easy if you use uksort().

    -----------------------------------
    uksort

    (PHP 3 >= 3.0.4, PHP 4, PHP 5)
    uksort -- Sort an array by keys using a user-defined comparison function
    -----------------------------------

    Read more here:


    Regards,
    Erwin Moller

    Comment

    • christiang@lateral.net

      #3
      Re: Sort array without index

      Hi Erwin, I tried in many ways, using also usort but it doesn't work.
      This is my last test:

      - iniside the method of the class -
      usort($arr, array ($this,"cmp"));

      function cmp($a, $b)
      {
      if ($a['relationCount_ int'] == $b['relationCount_ int']) {
      return 0;
      }
      return ($a['relationCount_ int'] > $b['relationCount_ int']) ? -1 : 1;
      }

      debugging it is comparing the right values, but the order doesn't
      change. Could be the fact that the array has as index a string instead
      of number? Cheers, chr

      Comment

      • christiang@lateral.net

        #4
        Re: Sort array without index

        Actually, I tried the sorting outside my class and it works properly:

        $arr = array();
        $arr['del.icio.us'] = array( "relationCount_ int" => 1, "name_str" =>
        "del.icio.u s");
        $arr['php'] = array( "relationCount_ int" => 1, "name_str" => "php");
        $arr['framework'] = array( "relationCount_ int" => 2, "name_str" =>
        "framework" );
        $arr['javascript'] = array( "relationCount_ int" => 3, "name_str" =>
        "javascript ");
        $arr['google'] = array( "relationCount_ int" => 1, "name_str" =>
        "google");
        $arr['xml'] = array( "relationCount_ int" => 1, "name_str" => "xml");
        $arr['RIA'] = array( "relationCount_ int" => 1, "name_str" => "RIA");
        $arr['library'] = array( "relationCount_ int" => 1, "name_str" =>
        "library");
        $arr['design'] = array( "relationCount_ int" => 1, "name_str" =>
        "design");
        $arr['development'] = array( "relationCount_ int" => 2, "name_str" =>
        "developmen t");
        $arr['webdesign'] = array( "relationCount_ int" => 1, "name_str" =>
        "webdesign" );
        $arr['web'] = array( "relationCount_ int" => 1, "name_str" => "web");

        function cmp($a, $b)
        {
        //print_r($a['relationCount_ int']." ==
        ".$b['relationCount_ int']."<br>");
        if ($a['relationCount_ int'] == $b['relationCount_ int']) {
        return 0;
        }
        return ($a['relationCount_ int'] > $b['relationCount_ int']) ? -1 : 1;
        }

        usort($arr, "cmp");


        print "<pre>";
        print_r($arr);
        print "</pre>";

        I have problem on modifying the array of my class, I tried even to
        remove the elements but I can't:

        foreach($this->relatedtags_ar r as $relatedtag){
        unset($this->relatedtags_ar r[$relatedtag['name_str']]);
        }

        Although the count() becomes 0, the elements are still there. How could
        modify that array, and how could I do inside my class? Any idea? Thanks
        a lot, chr

        Comment

        • F Laupretre

          #5
          Re: Sort array without index

          Le Tue, 07 Feb 2006 21:10:57 +0100, <christiang@lat eral.net> a écrit:
          [color=blue]
          > Actually, I tried the sorting outside my class and it works properly:[/color]
          ....[color=blue]
          > usort($arr, "cmp");[/color]


          If you want to keep the key associations, you must use uasort() instead of
          usort().
          [color=blue]
          > I have problem on modifying the array of my class, I tried even to
          > remove the elements but I can't:
          >
          > foreach($this->relatedtags_ar r as $relatedtag){
          > unset($this->relatedtags_ar r[$relatedtag['name_str']]);[/color]

          Why do you replicate your key in the value ? Seems very complex without
          need. You can keep only the key and sort on it, event with a user compare
          function (with uksort). But maybe the best solution would be the opposite
          method : removing the string keys and keeping the value field...

          You can compare on any field with the following code inside your class :

          var $field;
          var $a; // Array

          function sort_array($fie ld)
          {
          $this->field=$field ;
          uasort($a,$a,$t his->cmp);
          }

          function cmp($a1,$a2)
          {
          return strcmp($a1[$this->field],$a2[$this->field]);
          }
          //-- End of class

          And you sort your array with something like :

          $object->sort_array('fi eld_name');

          Comment

          Working...