【发布时间】:2017-10-23 20:09:53
【问题描述】:
我有一个名为 $row 的数组:
$row = array(
"comments" => "this is a test comment",
"current_path" => "testpath"
)
我还有另一个数组叫$dictionary:
$dictionary= array(
"comments" => "comments",
"current_directory" => "current_path"
)
我想将$row 中的键更改为与$dictionary 中的匹配值关联的键。
例如,在上述情况下,$row 将变为:
$row = array(
"comments" => "this is a test comment",
"current_directory" => "testpath"
)
我尝试过使用array_map,但这似乎并没有改变任何东西:
array_map(function($key) use ($dictionary){
return array_search($key, $dictionary);
}, array_keys($row));
如何正确更改密钥?
评论注释:
很遗憾,$dictionary 中的条目通常比 $row 多,并且无法保证顺序
【问题讨论】:
-
字典可以倒置吗?
-
@tereško 很遗憾没有
-
@tereško 虽然我可以在
$dictionary的副本上使用array_flip我想
标签: php arrays dictionary array-map