Arrays and references

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

    Arrays and references

    Hi

    I would like to append stuff to an array but doing it by reference -
    this is not working - can anyone help ?

    $anArray[]= "";

    $ref = &$anArray[];

    $ref = "a";
    $ref = "b";
    $ref = "c";

    anArray now contains a,b and c

    Thanks
    Tim

  • Rik

    #2
    Re: Arrays and references

    On Thu, 15 Feb 2007 15:52:28 +0100, TMN <nash.rsa@gmail .comwrote:
    I would like to append stuff to an array but doing it by reference -
    this is not working - can anyone help ?
    >
    $anArray[]= "";
    >
    $ref = &$anArray[];
    >
    $ref = "a";
    $ref = "b";
    $ref = "c";
    >
    anArray now contains a,b and c
    This cannot be done like that. $anArray[] is not 'the mystical place where
    variables go to be added to an array'. It's a construct that appends
    something as a last item to an array, and any reference to it will result
    in the actual value added at that point, not a 'new' array value.

    Just use $anArray[] = 'foo';, or possibly:

    $mainArray = array('foo' =bar,'foz'= array());
    $anArray = &$mainArray['foz'];
    $anArray[] = 'foo';
    $anArray[] = 'bar';

    Or even the array_push()/array_unshift() functions.


    What is the actual problem you're trying to solve here?
    --
    Rik Wasmus

    Comment

    • Toby A Inkster

      #3
      Re: Arrays and references

      TMN wrote:
      $anArray[]= "";
      >
      $ref = &$anArray[];
      >
      $ref = "a";
      $ref = "b";
      $ref = "c";
      $anArray[]= "";
      $ref = &$anArray[];
      $ref[] = "a";
      $ref[] = "b";
      $ref[] = "c";

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

      * = I'm getting there!

      Comment

      • TMN

        #4
        Re: Arrays and references

        On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
        wrote:
        TMN wrote:
        $anArray[]= "";
        >
        $ref = &$anArray[];
        >
        $ref = "a";
        $ref = "b";
        $ref = "c";
        >
        $anArray[]= "";
        $ref = &$anArray[];
        $ref[] = "a";
        $ref[] = "b";
        $ref[] = "c";
        >
        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~http://tobyinkster.co.uk/contact
        Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
        >
        * = I'm getting there!
        What I am trying to do is get a reference to an array so I can add
        elements to arrays via the ref.

        tim

        Comment

        • Kimmo Laine

          #5
          Re: Arrays and references

          "TMN" <nash.rsa@gmail .comwrote in message
          news:1171628462 .501867.158990@ t69g2000cwt.goo glegroups.com.. .
          On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
          wrote:
          >TMN wrote:
          $anArray[]= "";
          >>
          $ref = &$anArray[];
          >>
          $ref = "a";
          $ref = "b";
          $ref = "c";
          >>
          >$anArray[]= "";
          >$ref = &$anArray[];
          >$ref[] = "a";
          >$ref[] = "b";
          >$ref[] = "c";
          >>
          >--
          >Toby A Inkster BSc (Hons) ARCS
          >Contact Me ~http://tobyinkster.co.uk/contact
          >Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
          >>
          >* = I'm getting there!
          >
          What I am trying to do is get a reference to an array so I can add
          elements to arrays via the ref.

          You have to use the [] construct, there's no way around it. Yo can create a
          reference to the array, but you still need the [].

          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


          Comment

          • TMN

            #6
            Re: Arrays and references

            $anArray[]= "";
            $ref = &$anArray[];
            $ref[] = "a";
            $ref[] = "b";
            $ref[] = "c";

            But with this I am ending up with an array inside $anArray not just
            a,b and c ?

            Thanks
            Tim

            Comment

            • Kimmo Laine

              #7
              Re: Arrays and references

              "TMN" <nash.rsa@gmail .comwrote in message
              news:1171633666 .200970.46200@t 69g2000cwt.goog legroups.com...
              $anArray[]= "";
              $ref = &$anArray[];
              $ref[] = "a";
              $ref[] = "b";
              $ref[] = "c";
              >
              But with this I am ending up with an array inside $anArray not just
              a,b and c ?
              $anArray = array();
              $ref = &$anArray; // No [] here

              $ref[] = "a";
              $ref[] = "b";
              $ref[] = "c";

              --
              "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
              http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
              spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


              Comment

              • TMN

                #8
                Re: Arrays and references

                On Feb 16, 3:51 pm, "Kimmo Laine" <s...@outolempi .netwrote:
                "TMN" <nash....@gmail .comwrote in message
                >
                news:1171633666 .200970.46200@t 69g2000cwt.goog legroups.com...
                >
                $anArray[]= "";
                $ref = &$anArray[];
                $ref[] = "a";
                $ref[] = "b";
                $ref[] = "c";
                >
                But with this I am ending up with an array inside $anArray not just
                a,b and c ?
                >
                $anArray = array();
                $ref = &$anArray; // No [] here
                >
                $ref[] = "a";
                $ref[] = "b";
                $ref[] = "c";
                >
                --
                "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
                s...@outolempi. net | rot13(x...@bhgb yrzcv.arg)
                Thank you thank you !!!!!!!
                Tim

                Comment

                Working...