Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts

    largest numerical index in array?

    In PHP is there a function (or easy way not using a loop) to find the largest numerical index in an array?

    In javascript I can do

    Code:
    var a = new Array();
    a[5] = "x";
    alert(a.length); // 6 representing 0-5
    in php is there something like that?

    PHP Code:
    $a = array();
    $a[5] = "x";
    echo 
    array_length($a); // shows 6 (or even a function that shows 5) 
    Thanks,
    david_kw

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here ya go... the manual owns all: end

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure that is exactly what I'm looking for although that is my fallback position. Here is a more complex example.

    PHP Code:
    $a = array(); 
    $a[5] = "x"
    $a['y'] = "yes";
    echo 
    array_length($a); // shows 6 (or even a function that shows 5) 
    Wouldn't end point to 'y' in this case?

    What I'd like is something that does the following (but I don't know if it exists)

    PHP Code:
        public function checkArray() {
          
    $a[3] = "rt";
          
    $a[5] = "x";
          
    $a['y'] = "yes";
            
          
    $max 0;
          foreach (
    $a as $key => $value) {
            echo 
    "testing '$key': ";
            if (
    is_integer($key) && $max $key) {
              
    $max $key;
              echo 
    "is integer<br>";
            } else {
              echo 
    "is not integer<br>";
            }
          }
          echo 
    "max is '$max'<br>";
        }
      } 
    david_kw

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yea your looking for the last "key" correct? Well if you look at that page I showed you, the most recent comment shows you how.

    PHP Code:
    $lastkey end(array_keys($arr)); 

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well I guess that assumes your keys are all numbers. I would sort your array and then call that function.

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    $array = array("3" => "third""e" => "exactly""2" => "two""yes""no""for" => "each");

    print_r($array);
    //Array ( [3] => third [e] => exactly [2] => two [4] => yes [5] => no [for] => each )

    echo "<br /><br />";
    ksort($array);
    print_r($array);
    //Array ( [e] => exactly [for] => each [2] => two [3] => third [4] => yes [5] => no )

    echo "<br /><br />";
    echo 
    end(array_keys($array));
    //5 
    Outputs 5
    Last edited by iLLin; 03-30-2007 at 08:25 PM.

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like the exact functionality I wanted doesn't exist. Of course, there are some workarounds I can use including the ones you suggested.

    Thanks for the help. It can suck up a lot of time trying to find something that doesn't exist. :)

    david_kw

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok now go help me with my activeX object problem in the JS forum!

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    With problems like these, a different solution could maybe be more usable.

    Perhaps you are looking for the wrong thing.

    Code:
    var a = new Array();
    a[5] = "x";
    alert(a.length); // 6 representing 0-5
    This to me is illogical code.

    If I set a[5] to a value in an empty array, a.length should return 1. Because it only contains 1 element.

    What are you trying to achieve? Snippets might help.
    Last edited by aedrin; 03-30-2007 at 10:08 PM.

  • #10
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This to me is illogical code.
    I can understand what you mean, but this is working javascript code not necessarily how I want it to work (feel free to try it out).

    Code:
    <script type="text/javascript">
    var a = new Array();
    a[5] = "x";
    alert(a.length); // 6 representing 0-5
    </script>
    So since I learned javascript first I'm looking for similarities as I learn PHP. In JS, an array is numerical and the length property is updated as elements are added to the array. You can also add properties to an object which don't add to the overall length. So you can do

    Code:
    <script type="text/javascript">
    var a = new Array("a", "b", "c", "d");
    a['alphabetStart'] = true;
    for (var i = 0; i < a.length; i++) {
      alert(a[i]);
    }
    </script>
    And just get the numerical keys for the array (4 alerts with a-d). I could use count() to do something similar in PHP but I'd get a value of 5 since it has the extra element.

    Anyway, it isn't a big deal. The count() function will work fine in most cases and if I really need that functionality it is easy to write. I just wanted to know if there was already something written since PHP has tons of predefined functionality.

    david_kw
    Last edited by david_kw; 03-30-2007 at 10:33 PM.

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I can understand what you mean, but this is working javascript code not necessarily how I want it to work (feel free to try it out).
    I was mostly commenting on JavaScript's behaviour. I apologize if it looked like it was aimed as a comment on your code.

    Anyway, it isn't a big deal. The count() function will work fine in most cases and if I really need that functionality it is easy to write. I just wanted to know if there was already something written since PHP has tons of predefined functionality.
    Understood.

    That is usually the best way of doing things. Find out if PHP already does something like that. Mostly because PHP's functions would be much faster than writing your own. (Both coding time and execution time)

  • #12
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    There is a reason there is no function for it. Such a function would be pointless. Why would you need the largest numerical key? You don't need it to add a new value or to iterate over the array.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •