Voting

: max(zero, two)?
(Example: nine)

The Note You're Voting On

stacionari at gmail dot com
17 years ago
Sometimes you need to assigne values to an array index in some loop (if, while, foreach etc.) but you wish to set starting index key to some number greater then zero (lets say 5). One idea how to do this is:

<?php
$values
= array(5, 10, 15, 100); //array of values that we wish to add to our new array

$myArray = array(4=>0); //sets starting key to be 4 and assigns some value (lets say 0)
unset($myArray[4]); //delete this index key, but preserves further enumeration

foreach($values as $value){
$myArray[] = $value; //asign values to our array
}

print_r($myArray);

/* Output:

Array ( [5] => 5 [6] => 10 [7] => 15 [8] => 100 )

*/

?>

<< Back to user notes page

To Top