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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to get index of array?

    PHP Code:
    $myarray = array("one" => "ONE","two" => "TWO");

    foreach(
    $myarray as $position => $value){
        echo 
    index_version_of($position);


    Is there any way to get the numerical index of $position?

    i.e. instead of "one" get "1"?

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    how about just calling your variables like $something[1] instead of $something['one'] ?

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I need to have strings in both of them.

    I know the example I posted looks useless, but I need the concept of that.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You can do it this way:
    PHP Code:
    $myarray = array("one" => "ONE","two" => "TWO"); 
    foreach(
    $myarray as $position => $value)

        echo 
    array_search($positionarray_keys($myarray));

    But if you just want the position then use a counter like this:
    PHP Code:
    $myarray = array("one" => "ONE","two" => "TWO");

    $i 0;
    foreach(
    $myarray as $position => $value)

        echo 
    $i;
        
    $i++;



  •  

    Posting Permissions

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