Is there a php function which can take the below array
array (size=4)
1 => string '0'
6 => string '1'
7 => string '1'
8 => string '7'
And flip it to the below array notice that an array must have unique key values so can we flip the array where value 1 = key values 6, 7
array (size=3)
0 => string '1'
1 => string '6, 7'
7 => string '8'
$arr = array ( 1 => '0', 6 => '1', 7 => '1', 8 => '7' );
// Find unique values of array and make them as keys
$res = array_flip($arr);
// Find keys from sourse array with value of key in new array
foreach($res as $k =>$v) $res[$k] = implode(", ", array_keys($arr, $k));
result
Array
(
[0] => 1
[1] => 6, 7
[7] => 8
)
You can try it as
Simply use foreach
and interchange the values of key
with values
$arr = array ( 1 => '0', 6 => '1', 7 => '1', 8 => '7' );
$result = array();
foreach($arr as $key => $value){
$result[$value][] = $key;
}
print_r($result);
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With