Problem with updating a array member variable in function.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayphan
    New Member
    • Aug 2008
    • 4

    Problem with updating a array member variable in function.

    Hi

    I am new to php.

    I have a query. I have a class which has a array member variable list1 and list2. Am trying to update it in a function and not able to.

    Here is the class. Please help me.[code=php]
    <?php
    class simple {
    var $list1;
    var $list2;

    function updateValue() {
    $fd = fopen("/mapping", "r");
    $i=0;

    while(!feof($fd )) {
    $info = fscanf($fd, "%s %s\n");
    if ($info) {
    list($name, $version) =$info;
    $this->list1[$i] = $name;
    $this->list2[$i] = $version;
    $info = null;
    $i++;
    }
    }
    fclose($fd);
    echo "$this->list1[0]";
    echo "$this->list1[1]";
    echo "$this->list1[2]";
    }
    }
    ?>[/code]

    Thanks & Regards
    VP
    Last edited by pbmods; Aug 19 '08, 12:14 AM. Reason: Added CODE tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    What is it exactly that this code is doing?
    How is that different from what you expect it to do?

    Are you getting any errors?
    If not, see this topic.

    Comment

    • vijayphan
      New Member
      • Aug 2008
      • 4

      #3
      Hi

      I am getting the output as Array(0), Array(1) and Array(2). After executing the method.

      I have enabled the errors and it does not show anything. Is it the right way of assigning array values.

      Please help me.

      Regards,
      VP

      Comment

      • vijayphan
        New Member
        • Aug 2008
        • 4

        #4
        Originally posted by vijayphan
        Hi

        I am getting the output as Array(0), Array(1) and Array(2). After executing the method.

        I have enabled the errors and it does not show anything. Is it the right way of assigning array values.

        Please help me.


        Regards,
        VP
        Hi

        To be clear more with problem. I have a file with following values:

        dev0 1.2.3
        dev1 2.4.5
        dev2 2.6.7

        I want to read the dev0, dev1 and dev2 to get stored in list1 array and1.2.3, 2.4.5 and 2.6.7 in to list2 array of the class object.

        If list1 and list2 were local variables, I was able to get the desired output.
        When i use the class variables . I get the output as Array(0), Array(1) and Array(2) instead of dev0, dev1, dev2.

        Thanks
        VP

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Ahh ok.

          The quotes are causing the error.

          When you are echoing a variable, there is no need to surround it by quotes. It can, as in this case, even cause problems.

          Try doing:
          [code=php]
          echo $this->list[0];
          [/code]
          The reason why you got the output you did is because the parser doesn't accept the [0] behind the $this->list as a part of the variable.

          So, it prints $this->list, and because that is an array, it prints Array, followed by the [0].

          You can add array elements inside double-quotes tho, you just need to encapsulate them in curly-braces. Like:
          [code=php]
          echo "Value: {$this->list[0]}";
          echo "Value: {$this->list['something']";
          [/code]

          Also, when you are adding elements to an array the way you do, there is no need to specify the index of the element.

          Consider this:
          [code=php]
          while(!feof($fh )) {
          $this->list[] = fgets($fh);
          }
          [/code]
          This would create a zero-indexed array containing an element for each line in the file.

          Comment

          • vijayphan
            New Member
            • Aug 2008
            • 4

            #6
            Originally posted by Atli
            Ahh ok.

            The quotes are causing the error.

            When you are echoing a variable, there is no need to surround it by quotes. It can, as in this case, even cause problems.

            Try doing:
            [code=php]
            echo $this->list[0];
            [/code]
            The reason why you got the output you did is because the parser doesn't accept the [0] behind the $this->list as a part of the variable.

            So, it prints $this->list, and because that is an array, it prints Array, followed by the [0].

            You can add array elements inside double-quotes tho, you just need to encapsulate them in curly-braces. Like:
            [code=php]
            echo "Value: {$this->list[0]}";
            echo "Value: {$this->list['something']";
            [/code]

            Also, when you are adding elements to an array the way you do, there is no need to specify the index of the element.

            Consider this:
            [code=php]
            while(!feof($fh )) {
            $this->list[] = fgets($fh);
            }
            [/code]
            This would create a zero-indexed array containing an element for each line in the file.
            Hi Atli,

            It worked ... Thanks for the reply.

            Regards,
            VP

            Comment

            Working...