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.
<?php
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
?>
(PHP 4, PHP 5)
array_flip -- Exchanges all keys with their associated values in an arrayDescription
![Array Array](/uploads/1/3/7/5/137556028/954960362.jpg)
Note that the values of trans need to be valid keys, i.e. Volnay france map. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be flipped.
Description array arrayflip ( array trans ). Arrayflip returns an array in flip order, i.e. Reaction paper outline. Keys from trans become values and values from trans become keys. Note that the values of trans need to be valid keys, i.e. They need to be either integer or string.A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be flipped.
If a value has several occurrences, the latest key will be used as its values, and all others will be lost. Php empty.
Php Flip Array
- May 14, 2019 PHP arrayflip is an inbuilt function that flips/exchanges all keys with their associated values in the array. The arrayflip function returns the array in flip order; for example, keys from array become values and values from array become keys.
- Flip Array Given an array of positive elements, you have to flip the sign of some of its elements such that the resultant sum of the elements of array should be minimum non-negative (as close to zero as possible). Return the minimum no. Of elements whose sign needs to be flipped such that the resultant sum is minimum non-negative.