By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

Arrays and references

P: n/a
TMN
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

Feb 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rik
On Thu, 15 Feb 2007 15:52:28 +0100, TMN <na******@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
Feb 15 '07 #2

P: n/a
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!
Feb 15 '07 #3

P: n/a
TMN
On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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

Feb 16 '07 #4

P: n/a
"TMN" <na******@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 16 '07 #5

P: n/a
TMN
$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

Feb 16 '07 #6

P: n/a
"TMN" <na******@gmail.comwrote in message
news:11*********************@t69g2000cwt.googlegro ups.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
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 16 '07 #7

P: n/a
TMN
On Feb 16, 3:51 pm, "Kimmo Laine" <s...@outolempi.netwrote:
"TMN" <nash....@gmail.comwrote in message

news:11*********************@t69g2000cwt.googlegro ups.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...@bhgbyrzcv.arg)
Thank you thank you !!!!!!!
Tim

Feb 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.