I'm coming over from visual basic/ASP. I'm used to declaring arrays first, and redim-ming them if necessary.

In php you assign name value pairs like 1=>"value1", 2=>"value2", 3=> ...etc.

However, if for programming you need to declare another element out of sequence, say 89=>"highvalue" , then how do you find the upper bound of that array? i.e. how do I return the value index of the highest value (89)?


If you can answer that there's extra credit -- take a look at this:

1 => "Daniel"
2 => "Boone"
3 => "was a"
"TEXT" => "man"
5 => "yes a "
6 => "big man"

I know that $arrayName[3] will return "was a", and so forth, but how do I get "man" if I don't know the key "TEXT"? There must be some internal index as well that I'm not aware of.

I have to say that so far it's been discouraging working with the concepts in php and I haven't found anyone or any forum yet that gets into the 'mind' of php so that I can get the underlying concept.

Any suggestions?
Sam Fullman
sam@compasspointmedia.com

    To add a new entry to the end of the array you just assign a value without a subscript, php will pick the next index.

    To loop over all entries in the array you use the "each" function.

    There are many other array functions described in the documentation, I think you are trying too hard (you are trying to write VB code in php).

    <?php
    $x = array(1=>"value1", 2=>"value2");
    echo count($x);
    echo "<P>";
    $x[] = "value3";
    echo count($x);
    echo "<P>";

    while (list($key, $value) = each($x) ) {
    echo "$key $value <P>";
    }
    ?>
    Output is this:
    2
    3

    1 value1

    2 value2

    3 value3

      You could also use:

      foreach($arrayName as $key => $value) {

      print "$key is $value<br>\n";

      }

      To get the highest index value you
      could iterate over all the keys with the foreach loop.

        Write a Reply...