Skip to content

Instantly share code, notes, and snippets.

@iToto
Created December 13, 2012 20:28
Show Gist options
  • Save iToto/4279501 to your computer and use it in GitHub Desktop.
Save iToto/4279501 to your computer and use it in GitHub Desktop.
PHP function that creates key/value pair from a 2-dimensional array. NOTE: The original array must look something like this: Array ( [0] => Array ( [keyForKey] => KEY1 [keyForValue] => VALUE1 ) [1] => Array ( [keyForKey] => KEY2 [keyForValue] => VALUE2 ) )
<?php
/**
+ \brief convertArrayToDictionary
+
+ This function will take a 2-dimensional array with 2 keys in each index and convert it to a key-value pair array
+
+ \author Salvatore (Gimli) D'Agostino
+ \date Dec-13-2012
+ \param array (ARRAY) The array to convert (Must have 2 keys for every index)
+ \param key (STRING) The key of the key value (In the array param)
+ \param value (ARRAY) The key of the value index (In the array param)
+
+ \return (ARRAY) key/value pair
**/
private function convertArrayToDictionary($array,$key,$value)
{
$keyValue = array();
foreach ($array as $row) {
$keyValue[$row[$key]] = $row[$value];
}
return $keyValue;
}// END function convertArrayToDictionary
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment