one or two liners apprecaited!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    #1

    one or two liners apprecaited!

    Hi, I'm using PHP 4. I have two scalar variables, each containing
    comma separated lists of numbers

    $big_list = "1,2,3,4,5,6,7, 8";
    $small_list = "5,4,6";

    "$small_lis t" will always contain less elements than "$big_list" and
    all of "$small_lis t"s numbers will be included in "$big_list" . I want
    to get a variable that contains "$big_list" minus "$small_lis t". In
    the above example, the resulting varaible would contain "1,2,3,7,8" .

    Any short cuts are greatly appreciated.

    Thanks, - Dave

  • Oli Filth

    #2
    Re: one or two liners apprecaited!

    laredotornado@z ipmail.com said the following on 11/05/2006 15:32:[color=blue]
    > Hi, I'm using PHP 4. I have two scalar variables, each containing
    > comma separated lists of numbers
    >
    > $big_list = "1,2,3,4,5,6,7, 8";
    > $small_list = "5,4,6";
    >
    > "$small_lis t" will always contain less elements than "$big_list" and
    > all of "$small_lis t"s numbers will be included in "$big_list" . I want
    > to get a variable that contains "$big_list" minus "$small_lis t". In
    > the above example, the resulting varaible would contain "1,2,3,7,8" .[/color]

    It's easier to deal with arrays than strings, so convert your lists to
    arrays using explode(). Then use some combination of the PHP
    array-handling functions; array_diff() might do the job you're after.


    --
    Oli

    Comment

    • Ian Taylor

      #3
      Re: one or two liners apprecaited!

      laredotornado@z ipmail.com wrote:[color=blue]
      > Hi, I'm using PHP 4. I have two scalar variables, each containing
      > comma separated lists of numbers
      >
      > $big_list = "1,2,3,4,5,6,7, 8";
      > $small_list = "5,4,6";
      >
      > "$small_lis t" will always contain less elements than "$big_list" and
      > all of "$small_lis t"s numbers will be included in "$big_list" . I want
      > to get a variable that contains "$big_list" minus "$small_lis t". In
      > the above example, the resulting varaible would contain "1,2,3,7,8" .
      >
      > Any short cuts are greatly appreciated.
      >
      > Thanks, - Dave
      >[/color]

      This one-liner should work:

      print implode(",", array_diff(expl ode(",", $big_list), explode(",",
      $small_list)));

      However, if you're using it in code you (or someone else!) would need to
      maintain in the future, it would be wise to expand it to increase
      readability.

      Comment

      Working...