tags:

views:

40

answers:

4

I have a range of 67 numbers, something like 256 through to 323, which i want to add to an existing array. it doesnt matter what the values are.

looking for code to itterate through those numbers to add them as keys to the array without adding each one at a time

+1  A: 

you can use range() eg range(256,323)

ghostdog74
A: 

You can try using the range and array_merge functions.

Something like:

<?php

$arr = array(1,2,3); // existing array.
$new_ele = range(256,323); 

// add the new elements to the array.
$arr= array_merge($arr,$new_ele); 

var_dump($arr);

?>
codaddict
This would not add the values as keys.
Gordon
A: 

push(); could be a look worth, or you can do it like this

for($i=0;$i<count($array);$i++)
{
$anotherArray[$i] = $array[$i];

}
streetparade
+2  A: 

Try array_fill_keys and range

$existingArray =  array('foo', 'bar', 'baz');
$existingArray += array_fill_keys(range(256,323), null);

Use whatever you like instead of null. You could also use array_flip() instead of array_fill_keys(). You'd get the index keys as values then, e.g. 256 => 1, 257 => 2, etc.

Alternatively, use array_merge instead of the + operator. Depends on the outcome you want.

Gordon