Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
loosing is spelled losing - mickmackusa
Source Link
Nadir
  • 715
  • 8
  • 13

Easy stuff:

this function will accept the target $hash and $replacements is also a hash containing newkey=>oldkey associations.

This function will preserve original order, but could be problematic for very large (like above 10k records) arrays regarding performance & memory.

function keyRename(array $hash, array $replacements) { $new=array(); foreach($hash as $k=>$v) { if($ok=array_search($k,$replacements)) $k=$ok; $new[$k]=$v; } return $new; } 

this alternative function would do the same, with far better performance & memory usage, at the cost of loosinglosing original order (which should not be a problem since it is hashtable!)

function keyRename(array $hash, array $replacements) { foreach($hash as $k=>$v) if($ok=array_search($k,$replacements)) { $hash[$ok]=$v; unset($hash[$k]); } return $hash; } 

Easy stuff:

this function will accept the target $hash and $replacements is also a hash containing newkey=>oldkey associations.

This function will preserve original order, but could be problematic for very large (like above 10k records) arrays regarding performance & memory.

function keyRename(array $hash, array $replacements) { $new=array(); foreach($hash as $k=>$v) { if($ok=array_search($k,$replacements)) $k=$ok; $new[$k]=$v; } return $new; } 

this alternative function would do the same, with far better performance & memory usage, at the cost of loosing original order (which should not be a problem since it is hashtable!)

function keyRename(array $hash, array $replacements) { foreach($hash as $k=>$v) if($ok=array_search($k,$replacements)) { $hash[$ok]=$v; unset($hash[$k]); } return $hash; } 

Easy stuff:

this function will accept the target $hash and $replacements is also a hash containing newkey=>oldkey associations.

This function will preserve original order, but could be problematic for very large (like above 10k records) arrays regarding performance & memory.

function keyRename(array $hash, array $replacements) { $new=array(); foreach($hash as $k=>$v) { if($ok=array_search($k,$replacements)) $k=$ok; $new[$k]=$v; } return $new; } 

this alternative function would do the same, with far better performance & memory usage, at the cost of losing original order (which should not be a problem since it is hashtable!)

function keyRename(array $hash, array $replacements) { foreach($hash as $k=>$v) if($ok=array_search($k,$replacements)) { $hash[$ok]=$v; unset($hash[$k]); } return $hash; } 
Source Link
Nadir
  • 715
  • 8
  • 13

Easy stuff:

this function will accept the target $hash and $replacements is also a hash containing newkey=>oldkey associations.

This function will preserve original order, but could be problematic for very large (like above 10k records) arrays regarding performance & memory.

function keyRename(array $hash, array $replacements) { $new=array(); foreach($hash as $k=>$v) { if($ok=array_search($k,$replacements)) $k=$ok; $new[$k]=$v; } return $new; } 

this alternative function would do the same, with far better performance & memory usage, at the cost of loosing original order (which should not be a problem since it is hashtable!)

function keyRename(array $hash, array $replacements) { foreach($hash as $k=>$v) if($ok=array_search($k,$replacements)) { $hash[$ok]=$v; unset($hash[$k]); } return $hash; }