有没有我不知道的函数/方法可以避免在翻转数组时删除类似的键。示例如下:
原始数组:
Array ( [last_modified] => input [published] => input [project_content] => textarea )
使用数组翻转(键冲突):
Array ( [input] => published [textarea] => project_content )
最佳答案
如果你想保留你的 key ,你可以有一个二维数组:
<?php
$arr = array ( 'last_modified' => 'input', 'published' => 'input', 'project_content' => 'textarea' );
$result = array();
foreach($arr as $k => $v) {
if (array_key_exists($v, $result)) {
$result[$v][] = $k;
} else {
$result[$v] = array($k);
}
}
print_r($result);
?>
Array
(
[input] => Array
(
[0] => last_modified
[1] => published
)
[textarea] => Array
(
[0] => project_content
)
)
关于php - 阵列翻转碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18091481/