Re: Q: Array
| From: | Ivo Stoykov | Date: | Fri, 11 Jan 2002 10:28:56 +0000 |
| Subject: | Re: Q: Array | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-80181@lists.php.net to get a copy of this message | ||
Thank you Michel
You;ve understand correct the question. Thank you for the removing new
keyword
Everything you've said is exact but using following snippet
$b = array('one' => 'bla', 'two' => 'blabla',
'three' => 'blablabla');
while(list($k, $v) = each($b)) {
print "$k - $v";
}
the result will be
0 - bla
one - bla
0 (the key) = 'bla' (the value)
one (the key of the *same* element) = 'bla' (the *same* value)
and so on ...
"Michael Sims" <mhsims@midsouth.rr.com> wrote in message
news:5.1.0.14.0.20020110183411.03e18070@pop-server.midsouth.rr.com...
> At 09:18 PM 1/10/2002 +0100, Ivo Stoykov wrote:
> >How could I determine whether I have in the array's key integers *and*
> >strings or integers only?
>
> I'm not sure exactly what you're asking but I'll give it a shot...
>
> >i.e.
> >$a = new array('one', 'two', 'three'); // this has only
> >integers (am I
> >wrong?)
>
> First of all, the "new" keyword assumes that you are instantiating an
> object. For example if you created an object with the name foo you would
> instantiate it like so:
>
> $a = new foo;
>
> So when creating arrays the "new" keyword is undesirable.
>
> To address your question, yes your example above (after removing the "new"
> keyword) will create an array named $a that has only integers for
> keys. It's functionally the same as this:
>
> $a = array(0 => 'one', 1 => 'two', 2 => 'three');
>
> >$b = new array('one' => 'bla', 'two' =>
> >'blabla', 'three' =>
'blablabla');
> >// integers & strings
>
> The above example (again once corrected to remove the "new" keyword) will
> create an array named $b that has ONLY strings for keys. There are no
keys
> that are integers because you didn't create any. You can test this by
> trying to echo the values out by key:
>
> echo $b['one']; //outputs "bla"
> echo $b[0]; //outputs nothing because this key does not have a value.
>
> This is perfectly legal:
>
> $b = array ('one' => 'bla', 'two' => 'blabla',
> 'three' => 'blablabla', 0
=>
> 'blablablabla',
> 1 => 'blablablablabla');
>
> In the above case you do have some keys that are strings and some that are
> integers but they are DIFFERENT keys, representing DIFFERENT values.
>
> Hope that answers your question...
>