Re: Re: Ideas on modifying array keys
| From: | Ben Ramsey | Date: | Wed, 18 Feb 2004 17:38:02 +0000 |
| Subject: | Re: Re: Ideas on modifying array keys | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-178142@lists.php.net to get a copy of this message | ||
I know that it may be somewhat irrelevant to solving the problem at hand (and, in fact, I think your current method is probably the best method), but I was just wondering why you needed to change the names of your keys at all. Why is it not programmed so that the keys already have the names you want them to have?
Gerard Samuel wrote:
On Wednesday 18 February 2004 12:03 pm, Ben Ramsey wrote:You might be able to create some function that uses a combination of array_keys(), array_values(), and array_combine() to do what you want. Check the manual at http://www.php.net/array for more information on these functions.True, but I was looking to see if I could avoid running a loop, of which I would still have to loop over the result of array_keys() to modify its content. So for now, Im running -> function modify_key($array) {$array = (array)$array;foreach($array as $key => $value) { if (FALSE === ($pos = $strpos($key, '.'))) { continue; }$new_key = substr($key, $pos + 1); $array[ $new_key ] = $value; unset( $array[ $key ] ); }return $array;}