Results 1 to 4 of 4

Thread: Position of PHP Array

  1. #1
    Join Date
    Jun 2009
    Posts
    321

    Position of PHP Array

    I think that its fairly simple to add an element to an array in PHP but the problem is how do I determine the position of an element in a PHP array ?

    Also i would like to know how can i find the position of the PHP Array ? Please help me guys..

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Position of PHP Array

    An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

    Use array_search( ). It returns the key of the found element or false:
    $position = array_search($array, $value);
    if ($position !== false) {
    // the element in position $position has $value as its value in array $array
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Position of PHP Array

    key() returns the index element of the current array position.If you want to get the key name by position from Array than this will be the script:
    <?php
    $myArray['name1']=3;
    $myArray['name2']=2;
    $myArray['name3']=1;

    echo($myArray[1]); /* return NULL */
    /* isset($myArray[1]) return false; */
    /* is_null($myArray[1]) return true; */

    function KeyName($myArray,$pos) {
    // $pos--;
    /* uncomment the above line if you */
    /* prefer position to start from 1 */

    if ( ($pos < 0) || ( $pos >= count($myArray) ) )
    return "NULL"; // set this any way you like

    reset($myArray);
    for($i = 0;$i < $pos; $i++) next($myArray);

    return key($myArray);
    }

    echo KeyName($myArray,1); // result: name2
    echo KeyName($myArray,2); // result: name3
    echo KeyName($myArray,3); // result: "NULL"
    ?>

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Position of PHP Array

    However, because array_search () elegant studies in which the value is not found, it' s better to use array_search () instead of in_array ().

Similar Threads

  1. how can I know the position on a document?
    By Genuine in forum Windows Software
    Replies: 3
    Last Post: 22-03-2012, 06:24 PM
  2. Best way of observing you ads position
    By Ur Rehman in forum Technology & Internet
    Replies: 4
    Last Post: 20-01-2011, 11:35 AM
  3. How to fix div tag at a specific position
    By Leiff in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 07:23 PM
  4. CSS background position
    By Gomeler in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 12:54 PM
  5. Assigning an array to an array
    By MACE in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 05:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,709,518.12129 seconds with 16 queries