How to walk array and delete matched elements?

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

    #1

    How to walk array and delete matched elements?

    I'm kind of lost on this one - I need to modify 2 files based on user input:

    $data_array = file($data_file );
    $counter_array = file($counter_f ile);
    // There is a line-for-line relationship between the data and counter files
    //for example, if the 3rd line in the counter file is deleted,
    //so also must the 3rd line of the data file be deleted.
    // Each line of the data_file has 4 items:
    //date|ip_address |user_id|url
    for ( $i=0; $i < count($data_arr ay); $i++ )
    $data_line = fgets($data_arr ay); //look at each line in data_array
    $data = explode("|",$da ta_line);
    if ( $data[1] = $ip ) //$ip is user input
    {
    array_pop($data _array, $data_line);
    array_pop($coun ter_array[$i];
    //not sure array_pop is correct function here - the manual says this
    //will pop element from the bottom of the array, but I need
    //to delete this particular element from the middle of the array.
    //What is the best way to do this?
    }

    The goal is to create 2 new files. If the original files look like this:

    [data_file]
    3/4/04|20.123.112.2 1|grebos|http://www.asc.com
    6/5/04|183.40.99.14 |boris|http://www.asc.com
    7/16/04|133.73.115.2 9|grebos|http://www.asc.com

    [counter file]
    2345
    1356
    5466

    and the user selected to remove IP 183.40.99.14, then the new files should
    look like this:

    [data_file]
    3/4/04|20.123.112.2 1|grebos|http://www.asc.com
    7/16/04|133.73.115.2 9|grebos|http://www.asc.com

    [counter file]
    2345
    5466

    I'm still kind of green when it comes to working with arrays... any help is
    appreciated... Thanks.


  • Simon Stienen

    #2
    Re: How to walk array and delete matched elements?

    deko <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote:[color=blue]
    > I'm kind of lost on this one - I need to modify 2 files based on user input:[/color]
    8< -- long snip -- >8

    <http://de3.php.net/array_splice> should do the job.

    --
    Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

    Comment

    • deko

      #3
      Re: How to walk array and delete matched elements?

      > <http://de3.php.net/array_splice> should do the job.

      Yes, but when the matched element appears twice in a row (that is, in two
      lines immediately following each other in datafile.txt), the second one is
      not removed from the array. I think this is because the array pointer is
      being reset with each loop. Any suggestions on how to correct this?

      $data_array = file(datafile.t xt);
      $counter_file = file(counterfil e.txt);
      //$ip = user defined

      for ( $i=0; $i < count($data_arr ay); $i++ )
      {
      $data_line = $data_array[$i];
      $data = explode("|", $data_line); //data_line format:
      date|ip_address |user_id|url
      if( in_array($ip, $data) )
      {
      array_splice($d ata_array,$i,1) ;
      array_splice($c ounter_array,$i ,1);
      }
      }


      Comment

      • Simon Stienen

        #4
        Re: How to walk array and delete matched elements?

        deko <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote:[color=blue][color=green]
        >> <http://de3.php.net/array_splice> should do the job.[/color]
        >
        > Yes, but when the matched element appears twice in a row (that is, in two
        > lines immediately following each other in datafile.txt), the second one is
        > not removed from the array. I think this is because the array pointer is
        > being reset with each loop. Any suggestions on how to correct this?
        >
        > $data_array = file(datafile.t xt);
        > $counter_file = file(counterfil e.txt);
        > //$ip = user defined
        >
        > for ( $i=0; $i < count($data_arr ay); $i++ )
        > {
        > $data_line = $data_array[$i];
        > $data = explode("|", $data_line); //data_line format:
        > date|ip_address |user_id|url
        > if( in_array($ip, $data) )
        > {
        > array_splice($d ata_array,$i,1) ;
        > array_splice($c ounter_array,$i ,1);
        > }
        > }[/color]

        You mustn't increment after removing an element:

        if x == 'b' || x == 'c' -> remove:

        i=0.
        A B C D E
        ^-- not removed, i->1

        i=1
        A B C D E
        ^-- remove, i->2, data->A C D E

        i=2
        A C D E
        ^-- oops, missed the one which fills the hole the previosly deleted
        element left...

        --
        Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
        »What you do in this world is a matter of no consequence,
        The question is, what can you make people believe that you have done.«
        -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

        Comment

        • Chung Leong

          #5
          Re: How to walk array and delete matched elements?

          "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote in message
          news:bGu4d.1491 $nj.287@newssvr 13.news.prodigy .com...[color=blue]
          > I'm kind of lost on this one - I need to modify 2 files based on user[/color]
          input:[color=blue]
          >
          > $data_array = file($data_file );
          > $counter_array = file($counter_f ile);
          > // There is a line-for-line relationship between the data and counter[/color]
          files[color=blue]
          > //for example, if the 3rd line in the counter file is deleted,
          > //so also must the 3rd line of the data file be deleted.
          > // Each line of the data_file has 4 items:
          > //date|ip_address |user_id|url
          > for ( $i=0; $i < count($data_arr ay); $i++ )
          > $data_line = fgets($data_arr ay); //look at each line in data_array
          > $data = explode("|",$da ta_line);
          > if ( $data[1] = $ip ) //$ip is user input
          > {
          > array_pop($data _array, $data_line);
          > array_pop($coun ter_array[$i];
          > //not sure array_pop is correct function here - the manual says this
          > //will pop element from the bottom of the array, but I need
          > //to delete this particular element from the middle of the array.
          > //What is the best way to do this?
          > }
          >
          > The goal is to create 2 new files. If the original files look like this:
          >
          > [data_file]
          > 3/4/04|20.123.112.2 1|grebos|http://www.asc.com
          > 6/5/04|183.40.99.14 |boris|http://www.asc.com
          > 7/16/04|133.73.115.2 9|grebos|http://www.asc.com
          >
          > [counter file]
          > 2345
          > 1356
          > 5466
          >
          > and the user selected to remove IP 183.40.99.14, then the new files should
          > look like this:
          >
          > [data_file]
          > 3/4/04|20.123.112.2 1|grebos|http://www.asc.com
          > 7/16/04|133.73.115.2 9|grebos|http://www.asc.com
          >
          > [counter file]
          > 2345
          > 5466
          >
          > I'm still kind of green when it comes to working with arrays... any help[/color]
          is[color=blue]
          > appreciated... Thanks.
          >[/color]

          The function you need is array_filter().


          Comment

          • deko

            #6
            Re: How to walk array and delete matched elements?

            > The function you need is array_filter().

            Well, I came up with the below code which is kind of a brute force way to do
            it - but it works.
            I looked up array_filter in the manual - perhaps it could be used in place
            of the first if statement?

            <?php
            $data_file = "/path/to/data.txt";
            $counter_file = "/path/to/counter.txt";
            //need to reverse arrays because files are not the same length
            //but there is a line-to-line relationship starting from the bottom
            $data_array_asc ending = file($data_file );
            $data_array = array_reverse($ data_array_asce nding);
            $counter_array_ ascending = file($counter_f ile);
            $counter_array = array_reverse($ counter_array_a scending);
            $d = count($data_arr ay);
            echo $d." elements in data_array log\n\n";
            $ip = "68.122.35.157" ;
            while ( $d > $e )
            {
            $d = count($data_arr ay);
            for ( $i=0; $i < count($data_arr ay); $i++ )
            {
            $data_line = $data_array[$i];
            $data = explode("|", $data_line);
            //each line of data.txt has several elements separated by "|"
            if( in_array($ip, $data) )
            {
            array_splice($d ata_array,$i,1) ;
            array_splice($c ounter_array,$i ,1);
            $c++;
            $removed = $data_line.$rem oved;
            }
            }
            $e = count($data_arr ay);
            if ( $removed && $d == $e )
            {
            echo $c." elements removed:\n\n".$ removed."\n\n";
            }
            }
            echo $e." elements in data_array\n\n" ;
            $data_array_scr ubbed = array_reverse($ data_array);
            $fp = fopen($data_fil e,"w");
            foreach($data_a rray_scrubbed as $var)
            {
            fwrite($fp, $var);
            }
            fclose($fp);
            $counter_array_ scrubbed = array_reverse($ counter_array);
            $fp = fopen($counter_ file,"w");
            foreach($counte r_array_scrubbe d as $var)
            {
            fwrite($fp, $var);
            }
            fclose($fp);
            ?>


            Comment

            • Chung Leong

              #7
              Re: How to walk array and delete matched elements?

              "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote in message
              news:%tN4d.1821 $nj.1091@newssv r13.news.prodig y.com...[color=blue][color=green]
              > > The function you need is array_filter().[/color]
              >
              > Well, I came up with the below code which is kind of a brute force way to[/color]
              do[color=blue]
              > it - but it works.
              > I looked up array_filter in the manual - perhaps it could be used in place
              > of the first if statement?[/color]

              What I would do is this: Pass both arrays to array_map(), with null as the
              callback function. You will end up with an array with the following
              structure:

              Array
              (
              [0] => Array
              (
              [0] => 3/4/04|20.123.112.2 1|grebos|http://www.asc.com
              [1] => 2345
              )

              [1] => Array
              (
              [0] => 6/5/04|183.40.99.14 |boris|http://www.asc.com
              [1] => 5466
              )
              )

              Write a function that decides whether an element should be removed. It'll
              need to get the IP address via a global variable because array_filter()
              doesn't let you pass data to the callback. Or if you want to be more
              elegant, define a lamda function with create_function () that looks for that
              specific IP. Anyway, the function would look something like this:

              function NotMatchIP($a) {
              global $ip;
              $data_line = $a[0];
              $data = explode("|", $data_line);
              return ($ip != $data[1]);
              }

              Pass the combined array to array_filter() using this function as the
              callback. The array returned will contain the scrubbed content for both
              files. Just loop through it and write to both at the same time.

              The trick here is the use of array_map(). Working with one array is
              obviously easier than two.


              Comment

              • deko

                #8
                Re: How to walk array and delete matched elements?

                > What I would do is this: Pass both arrays to array_map(), with null as the[color=blue]
                > callback function. You will end up with an array with the following
                > structure:
                >
                > Array
                > (
                > [0] => Array
                > (
                > [0] => 3/4/04|20.123.112.2 1|grebos|http://www.asc.com
                > [1] => 2345
                > )
                >
                > [1] => Array
                > (
                > [0] => 6/5/04|183.40.99.14 |boris|http://www.asc.com
                > [1] => 5466
                > )
                > )[/color]

                So, this is a multi-dimensional array, correct?
                [color=blue]
                > Write a function that decides whether an element should be removed. It'll
                > need to get the IP address via a global variable because array_filter()
                > doesn't let you pass data to the callback. Or if you want to be more
                > elegant, define a lamda function with create_function () that looks for[/color]
                that[color=blue]
                > specific IP. Anyway, the function would look something like this:
                >
                > function NotMatchIP($a) {
                > global $ip;
                > $data_line = $a[0];
                > $data = explode("|", $data_line);
                > return ($ip != $data[1]);
                > }[/color]

                is this a lamda function? What is a lamda function?
                [color=blue]
                >
                > Pass the combined array to array_filter() using this function as the
                > callback. The array returned will contain the scrubbed content for both
                > files. Just loop through it and write to both at the same time.[/color]

                This sounds interesting - I'll give it a shot.
                [color=blue]
                > The trick here is the use of array_map(). Working with one array is
                > obviously easier than two.
                >[/color]


                Comment

                • Simon Stienen

                  #9
                  Re: How to walk array and delete matched elements?

                  deko <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote:[color=blue][color=green]
                  >> What I would do is this: Pass both arrays to array_map(), with null as the
                  >> callback function. You will end up with an array with the following
                  >> structure:
                  >>
                  >> Array
                  >> (
                  >> [0] => Array
                  >> (
                  >> [0] => 3/4/04|20.123.112.2 1|grebos|http://www.asc.com
                  >> [1] => 2345
                  >> )
                  >>
                  >> [1] => Array
                  >> (
                  >> [0] => 6/5/04|183.40.99.14 |boris|http://www.asc.com
                  >> [1] => 5466
                  >> )
                  >> )[/color]
                  >
                  > So, this is a multi-dimensional array, correct?[/color]

                  Correct.
                  [color=blue][color=green]
                  >> Write a function that decides whether an element should be removed. It'll
                  >> need to get the IP address via a global variable because array_filter()
                  >> doesn't let you pass data to the callback. Or if you want to be more
                  >> elegant, define a lamda function with create_function () that looks for
                  >> that specific IP. Anyway, the function would look something like this:
                  >>
                  >> function NotMatchIP($a) {
                  >> global $ip;
                  >> $data_line = $a[0];
                  >> $data = explode("|", $data_line);
                  >> return ($ip != $data[1]);
                  >> }[/color]
                  >
                  > is this a lamda function? What is a lamda function?[/color]

                  No. As soon as you use the control word "function" you have a named
                  function (here: NotMatchIP). A lambda function is an unnamed function,
                  created by create_function () and *only* by create_function ().
                  While you can call a named function using it's name (here:
                  MotMatchIp($foo )), you can't call a lambda function the same way, since it
                  has no name. Instead, you have a variable containing the function, calling
                  the variable with your parameters:
                  -----BEGIN PHP CODE BLOCK-----
                  $my_unnamed_fun ction = create_function ('$x','echo $x+1;');
                  $my_unnamed_fun ction(5); // outputs 6. notice the preceding dollar sign
                  $echo_next_inte ger = $my_unnamed_fun ction;
                  $echo_next_inte ger(17); // outputs 18.
                  ------END PHP CODE BLOCK------

                  I am not sure about this, but I guess, that internally the concept of PHPs
                  "function pointers" is used, like in:
                  -----BEGIN PHP CODE BLOCK-----
                  $trigonomical_f unction = 'sin'; // string!
                  echo $trigonomical_f unction($x); // calls sin($x)
                  ------END PHP CODE BLOCK------
                  Therefore echo $my_unnamed_fun ction should give you the internally used
                  name of the function. (Which is of no use, since it might change from one
                  parsing of the script to the next... It's just a technical detail.)

                  As you can see, you can "copy" the function to another variable, so you
                  could create whole bunches of functions, saving them in an array and
                  applying them one after another. All you have to do is to define a API of
                  parameters which will be used and returned:
                  -----BEGIN PHP CODE BLOCK-----
                  // define a function
                  function square($x) {
                  return $x*$x;
                  }

                  // list functions to use
                  $funcs = array(
                  'sin', // internal function
                  'square', // user-defined function
                  create_function ('$x','return $x*2') // lambda function
                  );

                  // output results
                  foreach ($funcs as $funcname)
                  echo $funcname . '(10) is: ' . $funcname(10) . chr(13);
                  ------END PHP CODE BLOCK------
                  (Note: Every of these functions takes exactly one number.)

                  HTH
                  Simon
                  --
                  Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
                  »What you do in this world is a matter of no consequence,
                  The question is, what can you make people believe that you have done.«
                  -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

                  Comment

                  • deko

                    #10
                    Re: How to walk array and delete matched elements?

                    > > is this a lamda function? What is a lamda function?[color=blue]
                    >
                    > No. As soon as you use the control word "function" you have a named
                    > function (here: NotMatchIP). A lambda function is an unnamed function,
                    > created by create_function () and *only* by create_function ().
                    > While you can call a named function using it's name (here:
                    > MotMatchIp($foo )), you can't call a lambda function the same way, since it
                    > has no name. Instead, you have a variable containing the function, calling
                    > the variable with your parameters:[/color]
                    .... snip ...[color=blue]
                    > I am not sure about this, but I guess, that internally the concept of PHPs
                    > "function pointers" is used, like in:[/color]
                    .... snip ...[color=blue]
                    > Therefore echo $my_unnamed_fun ction should give you the internally used
                    > name of the function. (Which is of no use, since it might change from one
                    > parsing of the script to the next... It's just a technical detail.)
                    >
                    > As you can see, you can "copy" the function to another variable, so you
                    > could create whole bunches of functions, saving them in an array and
                    > applying them one after another. All you have to do is to define a API of
                    > parameters which will be used and returned:[/color]

                    Thanks for the info - I appreciate the feedback.

                    The problem with using a multi-dimensional array is that the 2 files are
                    different lengths. I need to create both arrays individually to do stuff in
                    an earlier section of the script, so it's easy enough to reverse them and
                    start from the bottom. As long as the array keys stay in sync, it should
                    work...

                    $data_array_r = array_reverse($ data_array);
                    $counter_array_ r = array_reverse($ counter_array);
                    $d = count($data_arr ay_r) - 1; // there's a reason for -1
                    echo "Starting with ".$d." entries<br><br> ";
                    while ( $d > $s )
                    // $d>$s causes the while loop
                    //to run at least twice - so if there
                    //are 2 entries in a row in data_array
                    //that need to be spliced out,
                    //the second one isn't missed
                    //due to the array pointer reseting
                    //with each for loop.
                    {
                    $d = count($data_arr ay_r) - 1;
                    for ( $i=0; $i < $d; $i++ )
                    {
                    $data_line = $data_array_r[$i];
                    $data = explode("|", $data_line);
                    if( in_array($ip, $data) )
                    {
                    // here's where the 2 in a row problem arises
                    array_splice($d ata_array_r,$i, 1);
                    array_splice($c ounter_array_r, $i,1);
                    $n++;
                    $removed = $data_line."<br >".$removed;
                    }
                    }
                    $s = count($data_arr ay_r) - 1;
                    if ( $removed && $d == $s )
                    {
                    echo $n." entries removed:<br><br >".$removed."<b r>";
                    }
                    }
                    echo "Ending with ".$s." entries<br>";


                    Comment

                    • Chung Leong

                      #11
                      Re: How to walk array and delete matched elements?

                      "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote in message
                      news:3gJ5d.3491 $nj.1191@newssv r13.news.prodig y.com...[color=blue]
                      > The problem with using a multi-dimensional array is that the 2 files are
                      > different lengths. I need to create both arrays individually to do stuff[/color]
                      in[color=blue]
                      > an earlier section of the script, so it's easy enough to reverse them and
                      > start from the bottom. As long as the array keys stay in sync, it should
                      > work...[/color]

                      There is no problem at all. If the one array is shorter than the other,
                      array_map() would just leave the corresponding element null. It's easy
                      enough to check for null before writing out a line.


                      Comment

                      • deko

                        #12
                        Re: How to walk array and delete matched elements?

                        > There is no problem at all. If the one array is shorter than the other,[color=blue]
                        > array_map() would just leave the corresponding element null. It's easy
                        > enough to check for null before writing out a line.[/color]

                        Let's give this a shot - so I have 2 different length arrays that are mapped
                        (lets' call them c and d) and there is an element-to-element relationship
                        between the arrays. I need to walk through each element of array d,
                        exploding each element into another array (let's call it e) and check for a
                        particular value in e - if there's a match, I need to splice that element
                        out of both d and c. When this is done, I need to write out each array to a
                        separate file. Note that the d will have hundreds of elements, while c will
                        have thousands. The files from which the arrays are built are in ascending
                        order, but the-element to-element relationship starts at the bottom of the
                        files - so, for example, element 0 - 300 in d will map to 4000 - 4300 in c.

                        Okay, so first map the arrays:

                        $array_map = array_map(null, $c, $d)

                        Then, as suggested earlier, use a function to look for the element in
                        question in e (here's where I'm lost)

                        function findIp($???)
                        {
                        ???
                        }

                        then use this function in a for loop?

                        foreach ( $d as $var )
                        {
                        findIp($???)
                        )

                        then write out each array

                        $fpc = fopen($data_fil e, "w")'
                        $fpd = fopen($counter_ file, "w")
                        foreach($array_ map as ???)
                        {
                        fwrite($fpc, ???)
                        fwrite($fpd, ???)
                        }
                        fclose($fpc)
                        fclose($fpd)

                        how to use foreach with a multi-dimensional array???



                        Comment

                        • deko

                          #13
                          Re: How to walk array and delete matched elements?

                          I think this is close, but I get the following errors:

                          Warning: array_filter(): The second argument, '', should be a valid callback
                          in /test.php on line 84

                          Warning: Invalid argument supplied for foreach() in /test.php on line 89

                          Warning: Invalid argument supplied for foreach() in /test.php on line 94

                          if ( $ip )
                          {
                          $da = count($data_arr ay) - 1;
                          echo "Starting with ".$da." entries<br><br> ";
                          function noMatch($mapped _array)
                          {
                          global $ip;
                          $data_line = $mapped_array[0];
                          $data = explode("|", $data_line);
                          return ($ip != $data[1]);
                          }
                          $mapped_array = array_map(null, $data_array, $counter_array) ;
                          $array_scrub = array_filter($m apped_array, $noMatch); // line 84 *****
                          $sa = count($array_sc rub[0] - 1);
                          echo "Ending with ".$sa." entries<br><br> ";
                          $fpd = fopen($data_fil e, "w");
                          $fpc = fopen($counter_ file, "w");
                          foreach ( $array_scrub[0] as $d ) // line 89 *****
                          {
                          fwrite($d, $fpd);
                          }
                          fclose($fpd);
                          foreach ( $array_scrub[1] as $c ) // line 94 *****
                          {
                          fwrite($c, $fpc);
                          }
                          fclose($fpc);
                          unset($ip);
                          }


                          Comment

                          • deko

                            #14
                            Re: How to walk array and delete matched elements?

                            Using "matchIp" rather than "noMatch" - no errors, but results are incorrect

                            //----- code -----//

                            $da = count($data_arr ay);
                            echo "Starting with ".$da." entries<br><br> ";
                            function matchIp($mapped _array)
                            {
                            global $ip;
                            $data_line = $mapped_array[0];
                            $data = explode("|", $data_line);
                            return ($ip = $data[1]); //<<==== ** note change here **
                            }
                            $mapped_array = array_map(null, $data_array, $counter_array) ;
                            $array_scrub = array_filter($m apped_array, "matchIp");
                            $sa = count($array_sc rub);
                            echo "Ending with ".$sa." entries<br><br> ";

                            //----- output -----//

                            Starting with 148 entries

                            Ending with 148 entries


                            Comment

                            • deko

                              #15
                              Re: How to walk array and delete matched elements?

                              Not sure how to use the callback function. Also, how do I reference one
                              part of a multi-dimensional array?

                              //----- code -----//

                              echo "Starting with ".count($data_a rray)." entries<br><br> ";
                              function ipFilter($mappe d_array)
                              {
                              global $ip;
                              foreach ($mapped_array as $data_line)
                              {
                              $data_line = $mapped_array[0];
                              $data = explode("|", $data_line);
                              return ($ip != $data[1]);
                              }
                              }
                              $mapped_array = array_map(null, $data_array, $counter_array) ;
                              $array_scrub = array_filter($m apped_array, "ipFilter") ;
                              echo "Ending with ".count($data_a rray)." entries<br><br> ";

                              //----- output -----//

                              Starting with 148 entries

                              Ending with 148 entries ====>> this should be 1 less than starting number


                              Comment

                              Working...