note 107097 added to function.array-flip
| From: | kjensen at iaff106 dot com | Date: | Fri, 06 Jan 2012 18:23:09 +0000 |
| Subject: | note 107097 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185392@lists.php.net to get a copy of this message | ||
I needed a way to flip a multidimensional array and came up with this function to accomplish the
task. I hope it helps someone else.
function multi_array_flip($arrayIn, $DesiredKey, $DesiredKey2=false, $OrigKeyName=false) {
$ArrayOut=array();
foreach ($arrayIn as $Key=>$Value)
{
// If there is an original key that need to be preserved as data in the new array then do that if
requested ($OrigKeyName=true)
if ($OrigKeyName) $Value[$OrigKeyName]=$Key;
// Require a string value in the data part of the array that is keyed to $DesiredKey
if (!is_string($Value[$DesiredKey])) return false;
// If $DesiredKey2 was specified then assume a multidimensional array is desired and build it
if (is_string($DesiredKey2))
{
// Require a string value in the data part of the array that is keyed to $DesiredKey2
if (!is_string($Value[$DesiredKey2])) return false;
// Build NEW multidimensional array
$ArrayOut[$Value[$DesiredKey]][$Value[$DesiredKey2]]=$Value;
}
// Build NEW single dimention array
else $ArrayOut[$Value[$DesiredKey]][]=$Value;
}
return $ArrayOut;
}//end multi_array_flip
----
Server IP: 69.147.83.197
Probable Submitter: 75.244.105.167
----
Manual Page -- http://www.php.net/manual/en/function.array-flip.php
Edit -- https://master.php.net/note/edit/107097
Del: integrated -- https://master.php.net/note/delete/107097/integrated
Del: useless -- https://master.php.net/note/delete/107097/useless
Del: bad code -- https://master.php.net/note/delete/107097/bad+code
Del: spam -- https://master.php.net/note/delete/107097/spam
Del: non-english -- https://master.php.net/note/delete/107097/non-english
Del: in docs -- https://master.php.net/note/delete/107097/in+docs
Del: other reasons-- https://master.php.net/note/delete/107097
Reject -- https://master.php.net/note/reject/107097
Search -- https://master.php.net/manage/user-notes.php