php - ArrayIterator::valid

Description

bool ArrayIterator::valid ( void )
This function checks if the array contains any more entries.

Example 2294. ArrayIterator::valid() example

<?php

$array
= array('1' => 'one');
$arrayobject = new ArrayObject($array);$iterator = $arrayobject->getIterator();
var_dump($iterator->valid()); //bool(true)
$iterator->next(); // advance to the next item

//bool(false) because there is only one array element
 


var_dump($iterator->valid());

?>

php - ArrayIterator::rewind

Description

void ArrayIterator::rewind ( void )
This function rewinds the iterator to the beginning.

Example 2293. ArrayIterator::rewind() example

<?php

$arrayobject
= new ArrayObject();
$arrayobject[] = 'zero';$arrayobject[] = 'one';$arrayobject[] = 'two';
$iterator = $arrayobject->getIterator();
$iterator->next();
echo
$iterator->key(); //1
$iterator->rewind(); //rewinding to the beginingecho $iterator->key(); //0

?>

php - ArrayIterator::next

Description

void ArrayIterator::next ( void )
This function moves the iterator to the next entry.

Example 2292. ArrayIterator::next() example

<?php

$arrayobject
= new ArrayObject();
$arrayobject[] = 'zero';$arrayobject[] = 'one';
$iterator = $arrayobject->getIterator();

while(
$iterator->valid()) {
echo
$iterator->key() . ' => ' . $iterator->current() . "\n";

$iterator->next();
}

?>
  The above example will output:
0 => zero
1 => one

php - ArrayIterator::key

Description

mixed ArrayIterator::key ( void )
This function returns the current array key

Example 2291. ArrayIterator::key() example
<?php
$array
= array('key' => 'value');
$arrayobject = new ArrayObject($array);$iterator = $arrayobject->getIterator();

echo
$iterator->key(); //key 

?>

php - ArrayIterator::current

Description

mixed ArrayIterator::current ( void )
This function returns the current array entry

Example 2290. ArrayIterator::current() example

<?php
 
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');
$arrayobject = new ArrayObject($array);

for(
$iterator = $arrayobject->getIterator();
$iterator->valid();
$iterator->next()) {

echo
$iterator->key() . ' => ' . $iterator->current() . "\n";
}

?>
  The above example will output:
1 => one
2 => two
3 => three

php - array_walk_recursive

Description

bool array_walk_recursive ( array &$input, callback $funcname [, mixed $userdata] )
Applies the user-defined function funcname to each element of the input array. This function will recur into deeper arrays. Typically, funcname takes on two parameters. The input parameter's value being the first, and the key/index second. If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.
Returns TRUE on success or FALSE on failure.
Note: If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference. Then, any changes made to those elements will be made in the original array itself.

Example 285. array_walk_recursive() example
<?php
$sweet
= array('a' => 'apple', 'b' => 'banana');$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function
test_print($item, $key)
{
echo
"$key holds $item\n";
}
array_walk_recursive($fruits, 'test_print');

?>
  The above example will output:
a holds apple
b holds banana
sour holds lemon

php - array_walk

Description

bool array_walk ( array &$array, callback $funcname [, mixed $userdata] )
Returns TRUE on success or FALSE on failure.
Applies the user-defined function funcname to each element of the array array. Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second. If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.
If function funcname requires more parameters than given to it, an error of level E_WARNING will be generated each time array_walk() calls funcname. These warnings may be suppressed by prepending the PHP error operator @ to the array_walk() call, or by using error_reporting().
Note: If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference. Then, any changes made to those elements will be made in the original array itself.
Note: Passing the key and userdata to funcname was added in 4.0.0
array_walk() is not affected by the internal array pointer of array. array_walk() will walk through the entire array regardless of pointer position.
Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc. If the array that array_walk() is applied to is changed, the behavior of this function is undefined, and unpredictable.

Example 286. array_walk() example
<?php
$fruits
= array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function
test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}

function
test_print($item2, $key)
{
echo
"$key. $item2<br />\n";
}

echo
"Before ...:\n";array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo
"... and after:\n";
array_walk($fruits, 'test_print');

?>

 The above example will output:
Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

php - array_values

Description

array array_values ( array $input )
array_values() returns all the values from the input array and indexes numerically the array.

Example 284. array_values() example
<?php

$array
= array("size" => "XL", "color" => "gold");

print_r(array_values($array));

?>
  The above example will output:
Array
(
[0] => XL
[1] => gold
)

php - array_unshift

Description

int array_unshift ( array &$array, mixed $var [, mixed $...] )
array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
Returns the new number of elements in the array.

Example 283. array_unshift() example
<?php
$queue
= array("orange", "banana");

array_unshift($queue, "apple", "raspberry");

print_r($queue);

?>
  The above example will output:
Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)

php - array_unique

Description

array array_unique ( array $array )
array_unique() takes input array and returns a new array without duplicate values.
Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
The first element will be used.

Example 281. array_unique() example
<?php
$input
= array("a" => "green", "red", "b" => "green", "blue", "red");

$result = array_unique($input);
print_r($result); 
?> 

 The above example will output:
Array
(
[a] => green
[0] => red
[1] => blue
)


Example 282. array_unique() and types
<?php
$input
= array(4, "4", "3", 4, 3, "3"); 

$result = array_unique($input);
var_dump($result); 
?>
The above example will output:
array(2) {
[0] => int(4)
[2] => string(1) "3"
}