Reputation: 6974
With an array defined as...
$my_array = array (
'a' => array( 'BROWN' ),
'b' => array( 'GREEN', 'MIN_LEN' => 2, 'MAX_LEN' => 60, 'SOMETHING' )
);
Which looks like...
[a] => Array
(
[0] => BROWN
)
[b] => Array
(
[0] => GREEN
[MIN_LEN] => 2
[MAX_LEN] => 60
[1] => SOMETHING
)
How may I convert it to...
[a] => Array
(
[BROWN] => BROWN
)
[b] => Array
(
[GREEN] => GREEN
[MIN_LEN] => 2
[MAX_LEN] => 60
[SOMETHING] => SOMETHING
)
Notice the keys are the string value instead of numeric. OR it would be acceptable for the values to be null. eg [BROWN] => ''. So far all I can think of is array_flip, but I can't use that selectively.
Upvotes: 1
Views: 532
Reputation: 15903
You're going to need a user defined function. Something like:
function selective_flip(&$arr) {
foreach($arr as &$subarr) { //loops through a and b
foreach($subarr as $key => $value) {
if(is_string($value)) {
$subarr[$value] = $value;
unset($subarr[$key]);
}
}
}
}
Upvotes: 0
Reputation: 88707
foreach ($my_array as $oKey => $oVal) {
foreach ($oVal as $iKey => $iVal) {
if (!is_string($iKey) && is_string($iVal)) {
$my_array[$oKey][$iVal] = $iVal;
unset($my_array[$oKey][$iKey]);
}
}
}
Upvotes: 5