我在php中有以下数组 -
$arr1 = array("file1.php"=>"R1","file2.php"=>"R2", "file3.php"=>"R1");
如何获取相同值的键并打印不同的数组,如下面的
$arr2 = array("R1"=>"file1.php,file3.php","R2"=>"file2.php");
任何提示都受到高度赞赏。
答案 0 :(得分:2)
只需迭代数组并交换键值对(检查现有键)
$arr1 = array("file1.php"=>"R1","file2.php"=>"R2", "file3.php"=>"R1");
$arr2 = array();
foreach($arr1 as $key=>$value)
{
if(isset($arr2[$value]))
$arr2[$value] .= ",$key";
else
$arr2[$value] = $key;
}
print_r($arr2);