是否有一个我不知道的函数/方法可以在翻转数组时避免删除类似的键。下面的例子:
原始数组:
Array ( [last_modified] => input [published] => input [project_content] => textarea )
使用数组翻转(键冲突):
Array ( [input] => published [textarea] => project_content )
如果你想保留你的密钥,你可以有一个二维数组:
<?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
)
)