Re: perl developer struggling with some php array concepts
| From: | ("Richard Lynch") | Date: | Tue, 19 Dec 2000 23:33:27 +0000 |
| Subject: | Re: perl developer struggling with some php array concepts | ||
| References: | 1 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-31234@lists.php.net to get a copy of this message | ||
First, tell him that all PHP arrays are really hashes. There is really no
distinction between array types like in Perl.
Next, for this particular problem, I'd suggest doing this:
max(array_flip($a));
but only if there are no duplicates in the *VALUES* of the array. So this
would not work in the second case, only the first.
Another option would be to simply iterate through the array and check each
index, which may or may not be an integer in the first place -- So he may
need some additional code to ignore any non-integral values. This would
work for any array, so is the most general solution. But it could be slow
for a large array.
Finally, if this is coming from a real application, we'd need to know more
about the application to suggest another strategy to get what he needs. If
it's a contrived example... Well, there is no quick and easy way to do that
because it's not been needed :-) He could always write such a function as a
part of PHP and contribute it as a patch or pay somebody to do it.
count() simply tells how many items are in the array, with no effect from
what their indices are.
----- Original Message -----
From: Vinny <vinny@xaymaca.com>
Newsgroups: php.general
Sent: Tuesday, December 19, 2000 4:39 PM
Subject: [PHP] perl developer struggling with some php array concepts
> Hello,
> I'm forwarding this question to the group from a perl developer
> trying to grasp array concepts in php vs perl.
>
> His statement follows:
>
>
>
> Something anamolous in php: finding the highest index of an array is
> non-trivial. There should be some method available that tells what
> the highest index of an array is. In the first example it should be
> two, in the second example it should be 12.
>
> If anyone finds an answer to this, please share it with the group. This
> should be pretty basic.
>
>
> http://www.php.net/manual/function.count.php
>
> Example 1. Count() example
>
> $a[0] = 1;
> $a[1] = 3;
> $a[2] = 5;
> $result = count ($a);
> file://$result == 3, not 2, as there are 3 assigned elements
>
> $a[2] = 1;
> $a[4] = "";
> $a[6] = 5;
> $a[8] = 7;
> $a[10] = 11;
> $a[12] = "";
> $result = count ($a);
> // $result == 4, as there are 4 assigned element
>
>
> Thanks in advance.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net