Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Created January 8, 2019 00:07
Show Gist options
  • Save adamcrampton/d9a28ab7fe03e4d77070a89414aee6b3 to your computer and use it in GitHub Desktop.
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
/**
* 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