Sort question - make elements with = sign first

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    Sort question - make elements with = sign first


    Folks,

    I know I could do this with a foreach loop but it looks dirty. I'm
    wondering if I can do this via array_walk() or asort() and would appreciate
    some help..

    I have an array - an example follows

    $tmpArray[0]="one";
    $tmpArray[1]="two";
    $tmpArray[2]="three";
    $tmpArray[3]="apples";
    $tmpArray[4]="ref=1";

    The elements can be written up in any order, thus the last element "ref=1"
    could have been anywhere in the array, its just for this example that I have
    placed it as the last element.

    I need to read these elements, but I need to give precedence to first
    reading the elements that contain an equals sign before the others... It
    does *not* have to be alphabetically sorted - so the following result would
    be acceptable:

    $tmpArray[0]="ref=1";
    $tmpArray[1]="one";
    $tmpArray[2]="two";
    $tmpArray[3]="three";
    $tmpArray[4]="apples";

    Can anyone suggest/tell me a method that might be environmentally friendly
    to do this? I think array_walk might help but I've never worked out how to
    use it...

    Thanks
    randelld


  • Andy Hassall

    #2
    Re: Sort question - make elements with = sign first

    On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D."
    <you.can.email. me.at.randelld@ yahoo.com> wrote:
    [color=blue]
    >
    >Folks,
    >
    >I know I could do this with a foreach loop but it looks dirty. I'm
    >wondering if I can do this via array_walk() or asort() and would appreciate
    >some help..
    >
    >I have an array - an example follows
    >
    >$tmpArray[0]="one";
    >$tmpArray[1]="two";
    >$tmpArray[2]="three";
    >$tmpArray[3]="apples";
    >$tmpArray[4]="ref=1";
    >
    >The elements can be written up in any order, thus the last element "ref=1"
    >could have been anywhere in the array, its just for this example that I have
    >placed it as the last element.
    >
    >I need to read these elements, but I need to give precedence to first
    >reading the elements that contain an equals sign before the others... It
    >does *not* have to be alphabetically sorted - so the following result would
    >be acceptable:
    >
    >$tmpArray[0]="ref=1";
    >$tmpArray[1]="one";
    >$tmpArray[2]="two";
    >$tmpArray[3]="three";
    >$tmpArray[4]="apples";
    >
    >Can anyone suggest/tell me a method that might be environmentally friendly
    >to do this? I think array_walk might help but I've never worked out how to
    >use it...[/color]

    <pre>
    <?php
    $tmpArray[0]="one";
    $tmpArray[1]="two";
    $tmpArray[2]="three";
    $tmpArray[3]="apples";
    $tmpArray[4]="ref=1";

    function equalsSort($a,$ b) {
    if (strstr($a,'=') ) return -1;
    if (strstr($b,'=') ) return 1;
    return strcmp($a,$b);
    }

    usort($tmpArray , 'equalsSort');

    var_dump($tmpAr ray);
    ?>
    </pre>

    array(5) {
    [0]=>
    string(5) "ref=1"
    [1]=>
    string(6) "apples"
    [2]=>
    string(3) "one"
    [3]=>
    string(5) "three"
    [4]=>
    string(3) "two"
    }

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Maarten van der Peet

      #3
      Re: Sort question - make elements with = sign first

      Randell D. <you.can.email. me.at.randelld@ yahoo.com> wrote:
      [color=blue]
      > Folks,
      >
      > I know I could do this with a foreach loop but it looks dirty. I'm
      > wondering if I can do this via array_walk() or asort() and would appreciate
      > some help..[/color]

      You'll need usort.

      Sort an array by values using a user-defined comparison function


      <?php

      $list = array('one', 'two', '=three', 'four', '=five');
      print_array ($list);
      usort($list, "is_prec");
      print_array ($list);

      function is_prec ($a, $b) {
      if (substr($a,0,1) == '=' )
      {
      return -1;
      }
      elseif (substr($a,0,1) <> '=')
      {
      return 1;
      }
      else return 0;
      }

      function print_array($ar )
      {
      print "<pre>"; print_r($ar);pr int"</pre>";

      }
      ?>


      [color=blue]
      >
      > I have an array - an example follows
      >
      > $tmpArray[0]="one";
      > $tmpArray[1]="two";
      > $tmpArray[2]="three";
      > $tmpArray[3]="apples";
      > $tmpArray[4]="ref=1";
      >
      > The elements can be written up in any order, thus the last element "ref=1"
      > could have been anywhere in the array, its just for this example that I have
      > placed it as the last element.
      >
      > I need to read these elements, but I need to give precedence to first
      > reading the elements that contain an equals sign before the others... It
      > does *not* have to be alphabetically sorted - so the following result would
      > be acceptable:
      >
      > $tmpArray[0]="ref=1";
      > $tmpArray[1]="one";
      > $tmpArray[2]="two";
      > $tmpArray[3]="three";
      > $tmpArray[4]="apples";
      >
      > Can anyone suggest/tell me a method that might be environmentally friendly
      > to do this? I think array_walk might help but I've never worked out how to
      > use it...
      >
      > Thanks
      > randelld[/color]

      Goodluck.

      --
      Maarten van der Peet

      Comment

      • Randell D.

        #4
        Re: Sort question - make elements with = sign first


        "Maarten van der Peet" <Maarten.van.de r.Peet@haalditv etweg.xs4all.nl > wrote
        in message
        news:1g0zrl6.1j 3vurcbn178N%Maa rten.van.der.Pe et@haalditvetwe g.xs4all.nl...[color=blue]
        > Randell D. <you.can.email. me.at.randelld@ yahoo.com> wrote:
        >[color=green]
        > > Folks,
        > >
        > > I know I could do this with a foreach loop but it looks dirty. I'm
        > > wondering if I can do this via array_walk() or asort() and would[/color][/color]
        appreciate[color=blue][color=green]
        > > some help..[/color]
        >
        > You'll need usort.
        >
        > http://nl.php.net/manual/en/function.usort.php
        >
        > <?php
        >
        > $list = array('one', 'two', '=three', 'four', '=five');
        > print_array ($list);
        > usort($list, "is_prec");
        > print_array ($list);
        >
        > function is_prec ($a, $b) {
        > if (substr($a,0,1) == '=' )
        > {
        > return -1;
        > }
        > elseif (substr($a,0,1) <> '=')
        > {
        > return 1;
        > }
        > else return 0;
        > }
        >
        > function print_array($ar )
        > {
        > print "<pre>"; print_r($ar);pr int"</pre>";
        >
        > }
        > ?>
        >
        >
        >[color=green]
        > >
        > > I have an array - an example follows
        > >
        > > $tmpArray[0]="one";
        > > $tmpArray[1]="two";
        > > $tmpArray[2]="three";
        > > $tmpArray[3]="apples";
        > > $tmpArray[4]="ref=1";
        > >
        > > The elements can be written up in any order, thus the last element[/color][/color]
        "ref=1"[color=blue][color=green]
        > > could have been anywhere in the array, its just for this example that I[/color][/color]
        have[color=blue][color=green]
        > > placed it as the last element.
        > >
        > > I need to read these elements, but I need to give precedence to first
        > > reading the elements that contain an equals sign before the others... It
        > > does *not* have to be alphabetically sorted - so the following result[/color][/color]
        would[color=blue][color=green]
        > > be acceptable:
        > >
        > > $tmpArray[0]="ref=1";
        > > $tmpArray[1]="one";
        > > $tmpArray[2]="two";
        > > $tmpArray[3]="three";
        > > $tmpArray[4]="apples";
        > >
        > > Can anyone suggest/tell me a method that might be environmentally[/color][/color]
        friendly[color=blue][color=green]
        > > to do this? I think array_walk might help but I've never worked out how[/color][/color]
        to[color=blue][color=green]
        > > use it...
        > >
        > > Thanks
        > > randelld[/color]
        >
        > Goodluck.
        >
        > --
        > Maarten van der Peet[/color]

        I don't think you read my entire post properly... I do not have any element
        that begins with an = sign, instead, I have *some* elements that *have* an
        equals sign somewhere. The array you created in your example is not the
        same as the array in my question.

        Thanks for trying though.


        Comment

        • Randell D.

          #5
          Re: Sort question - make elements with = sign first

          "Andy Hassall" <andy@andyh.co. uk> wrote in message
          news:uvsplv4b00 5ouoiojm4snjml1 s0if10ato@4ax.c om...[color=blue]
          > On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D."
          > <you.can.email. me.at.randelld@ yahoo.com> wrote:
          >[color=green]
          > >
          > >Folks,
          > >
          > >I know I could do this with a foreach loop but it looks dirty. I'm
          > >wondering if I can do this via array_walk() or asort() and would[/color][/color]
          appreciate[color=blue][color=green]
          > >some help..
          > >
          > >I have an array - an example follows
          > >
          > >$tmpArray[0]="one";
          > >$tmpArray[1]="two";
          > >$tmpArray[2]="three";
          > >$tmpArray[3]="apples";
          > >$tmpArray[4]="ref=1";
          > >
          > >The elements can be written up in any order, thus the last element[/color][/color]
          "ref=1"[color=blue][color=green]
          > >could have been anywhere in the array, its just for this example that I[/color][/color]
          have[color=blue][color=green]
          > >placed it as the last element.
          > >
          > >I need to read these elements, but I need to give precedence to first
          > >reading the elements that contain an equals sign before the others... It
          > >does *not* have to be alphabetically sorted - so the following result[/color][/color]
          would[color=blue][color=green]
          > >be acceptable:
          > >
          > >$tmpArray[0]="ref=1";
          > >$tmpArray[1]="one";
          > >$tmpArray[2]="two";
          > >$tmpArray[3]="three";
          > >$tmpArray[4]="apples";
          > >
          > >Can anyone suggest/tell me a method that might be environmentally[/color][/color]
          friendly[color=blue][color=green]
          > >to do this? I think array_walk might help but I've never worked out how[/color][/color]
          to[color=blue][color=green]
          > >use it...[/color]
          >
          > <pre>
          > <?php
          > $tmpArray[0]="one";
          > $tmpArray[1]="two";
          > $tmpArray[2]="three";
          > $tmpArray[3]="apples";
          > $tmpArray[4]="ref=1";
          >
          > function equalsSort($a,$ b) {
          > if (strstr($a,'=') ) return -1;
          > if (strstr($b,'=') ) return 1;
          > return strcmp($a,$b);
          > }
          >
          > usort($tmpArray , 'equalsSort');
          >
          > var_dump($tmpAr ray);
          > ?>
          > </pre>
          >
          > array(5) {
          > [0]=>
          > string(5) "ref=1"
          > [1]=>
          > string(6) "apples"
          > [2]=>
          > string(3) "one"
          > [3]=>
          > string(5) "three"
          > [4]=>
          > string(3) "two"
          > }
          >
          > --
          > Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
          > Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)[/color]

          Cheers - that works perfectly... and looks tidy too...


          Comment

          • Maarten van der Peet

            #6
            Re: Sort question - make elements with = sign first

            Randell D. <you.can.email. me.at.randelld@ yahoo.com> wrote:

            <snip>[color=blue]
            > I don't think you read my entire post properly... I do not have any element
            > that begins with an = sign, instead, I have *some* elements that *have* an
            > equals sign somewhere. The array you created in your example is not the
            > same as the array in my question.
            >[/color]
            Yes you're right. I must have had a black out. :-)

            But the understanding and using of 'usort' is still valid.
            [color=blue]
            > Thanks for trying though.[/color]

            You're welkom.

            --
            Maarten van der Peet

            Comment

            Working...