List of Array Functions

each()

Return the current key and value pair from an array and advance the array cursor.

After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.

For an example of see “Iterate Array Using While”.

end()

end() advances array’s internal pointer to the last element, and returns its value.

$lastSP = end($SP_arr);

 

prev()

prev() behaves just like next(), except it rewinds the internal array pointer one place instead of advancing it. Note: You won’t be able to distinguish the beginning of an array from a boolean FALSE element. To properly traverse an array which may contain FALSE elements, see the each() function.

// example of backward iteration

$ar = array ( 'a', 'b', 'c', 'd', 'e', 'f') ;

print_r($ar);

end($ar);
while($val = current($ar)) {
  echo $val.' ';
  prev($ar);
}

 

reset()

reset() rewinds array’s internal pointer to the first element and returns the value of the first array element./p>

next()

next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one.

$mode = next($transport);

 

current()

The current() function simply returns the value of the array element that’s currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE.

key()

key() returns the index element of the current array position.

$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}

 

is_array()

Takes a single argument of any type and returns a true value if the argument is an array, and false otherwise. See example:

if (!isset($vehicle_in) OR !is_array($vehicle_in) OR sizeof($vehicle_in) < 1) {
  die("Function failed to create select box because no array was passed.");
}

 

count()

Takes an array as argument and returns the number of nonempty elements in the array. (This will be 1 for strings and numbers.)

sizeof()

Identical to count().

in_array()

Takes two arguments: an element (that might be a value in an array), and an array (that might contain the element). Returns true if the element is contained as a value in the array, false otherwise. (Note that this does not test for the presence of keys in the array.)

IsSet($array[$key])

Takes an array[key] form and returns true if the key portion is a valid key for the array. (This is a specific use of the more general function IsSet(), which tests whether a variable is bound.)

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Coding, PHP Arrays and tagged , , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s