Last active
August 29, 2015 14:11
-
-
Save WesleyE/dda32ca880992dff1429 to your computer and use it in GitHub Desktop.
Removes a whole column from a multidimensional resultset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Removes a whole column from a multidimensional resultset. | |
* You could use the array_splice function, this is a bit slower (10.3%) but defragments the empty positions | |
* in the internal php array. Use this when removing a lot of columns on big (huge) resultsets. | |
* | |
* @param $aArray | |
* @param $sKey | |
* @return bool | |
*/ | |
function deleteMultidimensionalColumn(&$aArray, $sKey, $bUseSplice = false) { | |
if(count($aArray) > 0 && array_key_exists($sKey, $aArray[0])) { | |
if ($bUseSplice) { | |
$iOffset = array_flip(array_keys($aArray[0]))[$sKey]; | |
} | |
return array_walk($aArray, function (&$mVal) use ($sKey, $bUseSplice) { | |
if($bUseSplice) { | |
// 10.3% langzamer, behoud wel netjes interne array structuur, dus voor | |
//grotere array's zou dit minder (interne) fragmentatie op moeten leveren. | |
array_splice($mVal, $iOffset, 1); | |
} else { | |
unset($mVal[$sKey]); | |
} | |
}); | |
} else { | |
return array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment