Created
January 8, 2019 00:07
-
-
Save adamcrampton/d9a28ab7fe03e4d77070a89414aee6b3 to your computer and use it in GitHub Desktop.
Flatten a multidimensional PDO array into an indexed array when fetching a single column
This file contains hidden or 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
/** | |
* Flatten array of PDO results into a single dimension. | |
* Note: This is only useful for a PDO query returning a single column (you probably don't want to do this otherwise). | |
* | |
* @param array $arrayToFlatten | |
* @return array | |
*/ | |
public function flattenArray($arrayToFlatten, $key) | |
{ | |
// Set up array. | |
$newArray = []; | |
foreach ($arrayToFlatten as $index => $value) { | |
$newArray[$index] = $value[$key]; | |
} | |
return $newArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment