Help with arrays

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

    Help with arrays

    Hi,
    I have problems in trying to '++' an array value.
    Every time I find a city I add it in the array,
    and if I find it many times I increment the value (counter)
    of the city. Here's the code:


    $address = "http://www.cji.co.il/bw040609.txt";
    $address_local = "bw040609.t xt";
    $lines = file($address_l ocal);

    $heading = "Companies covered in this CJI report:";
    $heading_line = 0;

    $towns = array();

    for($i=0; $i<count($lines ); $i++) {

    $pos = strpos($lines[$i], "Location: ");
    if($pos !== false) {
    list($loc,$town ) = preg_split("/: /", $lines[$i]);
    if(in_array($to wn, $towns)) {
    $towns[$town] = 1;
    echo "Town added: $town<br>";

    }
    else {
    $towns[$town]++;
    echo "Town added: $town<br>";
    }
    }

    }


    foreach($towns as $key => $value) {
    print "$key => $value<br>";
    }



    The output is like this:

    Notice: Undefined index: Ramat Hasharon in
    C:\Programs\wor k\jobs\index.ph p on line 40
    Town added: Ramat Hasharon

    Notice: Undefined index: North in C:\Programs\wor k\jobs\index.ph p on line 40
    Town added: North
    Town added: North

    Notice: Undefined index: Location in C:\Programs\wor k\jobs\index.ph p on
    line 40
    Town added: Location
    Town added: Location
    Town added: North
    Town added: North

    and so on...

    It complains about an undefined index.
    Any idea why it does that?

    Shmuel.
  • Pedro Graca

    #2
    Re: Help with arrays

    Shmuel wrote:
    (snip)[color=blue]
    > if(in_array($to wn, $towns)) {[/color]

    Shouldn't this be reversed?

    if(!in_array($t own, $towns)) {


    [color=blue]
    > $towns[$town] = 1;
    > echo "Town added: $town<br>";
    >
    > }
    > else {
    > $towns[$town]++;
    > echo "Town added: $town<br>";
    > }[/color]
    (snip)


    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • Shmuel

      #3
      Re: Help with arrays

      Then the result will be 1 for each town even if there are many hits.


      Pedro Graca wrote:
      [color=blue]
      > Shmuel wrote:
      > (snip)
      >[color=green]
      >> if(in_array($to wn, $towns)) {[/color]
      >
      >
      > Shouldn't this be reversed?
      >
      > if(!in_array($t own, $towns)) {
      >
      >
      >
      >[color=green]
      >> $towns[$town] = 1;
      >> echo "Town added: $town<br>";
      >>
      >> }
      >> else {
      >> $towns[$town]++;
      >> echo "Town added: $town<br>";
      >> }[/color]
      >
      > (snip)
      >
      >[/color]

      Comment

      • Pedro Graca

        #4
        Re: Help with arrays

        Shmuel wrote:[color=blue]
        > Then the result will be 1 for each town even if there are many hits.[/color]


        use array_key_exist s() instead of in_array()

        Checks if the given key or index exists in the array


        ========
        pedro$ cat xx.php
        <?php
        $towns = array();
        $towns['x'] = 3;

        $town = 'x';
        if(!array_key_e xists($town, $towns)) {
        $towns[$town] = 1;
        echo "Town added: $town<br>\n";
        } else {
        $towns[$town]++;
        echo "Town added: $town<br>\n";
        }

        $town = 'y';
        if(!array_key_e xists($town, $towns)) {
        $towns[$town] = 1;
        echo "Town added: $town<br>\n";
        } else {
        $towns[$town]++;
        echo "Town added: $town<br>\n";
        }

        print_r($towns) ;
        ?>

        pedro$ php xx.php
        Town added: x<br>
        Town added: y<br>
        Array
        (
        [x] => 4
        [y] => 1
        )
        ========

        --
        USENET would be a better place if everybody read: | to email me: use |
        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
        http://www.expita.com/nomime.html | no attachments. |

        Comment

        • Christian Fersch

          #5
          Re: Help with arrays

          Shmuel wrote:[color=blue]
          > Pedro Graca wrote:[color=green]
          >> Shouldn't this be reversed?[/color]
          >
          > Then the result will be 1 for each town even if there are many hits.[/color]

          No, Pedro is right - your code is wrong, or isn't it 1 for each town
          even if there are many hits?

          Greetings Christian

          Comment

          • Chung Leong

            #6
            Re: Help with arrays

            Do this:

            for($i=0; $i<count($lines ); $i++) {

            $pos = strpos($lines[$i], "Location: ");
            if($pos !== false) {
            list($loc,$town ) = preg_split("/: /", $lines[$i]);
            @$towns[$town] += 1;
            }

            }


            "Shmuel" <sdg@nic.fi> wrote in message
            news:lEiyc.2174 $FM3.757@reader 1.news.jippii.n et...[color=blue]
            > Hi,
            > I have problems in trying to '++' an array value.
            > Every time I find a city I add it in the array,
            > and if I find it many times I increment the value (counter)
            > of the city. Here's the code:
            >
            >
            > $address = "http://www.cji.co.il/bw040609.txt";
            > $address_local = "bw040609.t xt";
            > $lines = file($address_l ocal);
            >
            > $heading = "Companies covered in this CJI report:";
            > $heading_line = 0;
            >
            > $towns = array();
            >
            > for($i=0; $i<count($lines ); $i++) {
            >
            > $pos = strpos($lines[$i], "Location: ");
            > if($pos !== false) {
            > list($loc,$town ) = preg_split("/: /", $lines[$i]);
            > if(in_array($to wn, $towns)) {
            > $towns[$town] = 1;
            > echo "Town added: $town<br>";
            >
            > }
            > else {
            > $towns[$town]++;
            > echo "Town added: $town<br>";
            > }
            > }
            >
            > }
            >
            >
            > foreach($towns as $key => $value) {
            > print "$key => $value<br>";
            > }
            >
            >
            >
            > The output is like this:
            >
            > Notice: Undefined index: Ramat Hasharon in
            > C:\Programs\wor k\jobs\index.ph p on line 40
            > Town added: Ramat Hasharon
            >
            > Notice: Undefined index: North in C:\Programs\wor k\jobs\index.ph p on line[/color]
            40[color=blue]
            > Town added: North
            > Town added: North
            >
            > Notice: Undefined index: Location in C:\Programs\wor k\jobs\index.ph p on
            > line 40
            > Town added: Location
            > Town added: Location
            > Town added: North
            > Town added: North
            >
            > and so on...
            >
            > It complains about an undefined index.
            > Any idea why it does that?[/color]



            Comment

            Working...