Jump to: navigation, search

Php each

From w3cyberlearnings

Contents

PHP function each

This function returns four elements, and it contains key and value for the current array pointer.

Syntax each

  • array: array input
each(array);

Return

  • Key
    • [0]=>key1
    • [key]=>key1
  • Value
    • [1] => value1
    • [value]=>value1

Example 1:Associative array

<?php

$name_score = array(
	 'Ella' => 30,
	 'Amelia' => 2,
	 'Liam' => 21
);

$e_name = each($name_score);
print_r($e_name);
?>

Output

  • [0]= Ella, [1]= 30
Array
(
    [1] => 30
    [value] => 30
    [0] => Ella
    [key] => Ella
)

Example 2

  • Normal array has index starts from 0.
  • Bob has index 0,
    • Bob key's is [0] (i.e Index 0)
    • Bob value's is [1]
<?php

$name = array('bob', 'john', 'lilo');
$e_name = each($name);

print_r($e_name);
?>

Output


Array
(
    [1] => bob
    [value] => bob
    [0] => 0
    [key] => 0
)

Example 3

<?php

$array = array(
	 'Semester 1',
	 'Semester 2',
	 'Semester 3',
	 'Semester 4',
	 'Semester 5',
	 'Semester 6',
	 'Semester 7',
	 'Semester 8'
);

// skip two semesters
next($array); // 
next($array); // 
// semeter 3
$a = each($array);
print_r($a);
echo "<br/>";
echo current($array); // semester 4
echo "<br/>";
// skip three semesters
next($array); //
next($array); //
next($array); //

$b = each($array);
print_r($b);
?>

Output


Array
(
    [1] => Semester 3
    [value] => Semester 3
    [0] => 2
    [key] => 2
)
Semester 4
Array
(
    [1] => Semester 7
    [value] => Semester 7
    [0] => 6
    [key] => 6
)

Related Links


Navigation
Web
SQL
MISC
References