associative to numeric array
Posted: Wed Feb 24, 2010 4:32 pm
Hi,
I need something hopefully simple. I have an associative array and I need to change it to a numerical array. Basically change the key association from string to numeric. Or, be able to index an associative by a numeric index.
start with...
$arr = array ("key1" => "value1", "key2" => "value2" .... etc);
what I need to have is a way to get at the second element by number. For instance, shuffle converts the key association to numeric, which is OK since its suppose to be random. then I can access it something like : "$some_value = $arr[rand(0, count($arr) -1];" (This may be a bad example because here I could do a shuffle($arr); $val = $arr[0]. BUT. id lose the string keys. no?)
I don't want to use an iterator. That's what I am doing now and it sucks.
cheers,
Mike
I need something hopefully simple. I have an associative array and I need to change it to a numerical array. Basically change the key association from string to numeric. Or, be able to index an associative by a numeric index.
start with...
$arr = array ("key1" => "value1", "key2" => "value2" .... etc);
what I need to have is a way to get at the second element by number. For instance, shuffle converts the key association to numeric, which is OK since its suppose to be random. then I can access it something like : "$some_value = $arr[rand(0, count($arr) -1];" (This may be a bad example because here I could do a shuffle($arr); $val = $arr[0]. BUT. id lose the string keys. no?)
I don't want to use an iterator. That's what I am doing now and it sucks.
cheers,
Mike