PHP索引数组到关联的第一个元素等于第二个,依此类推

时间:2017-08-30 17:25:14

标签: php arrays

我有一个这样的数组:

$actions = array(
   "controller",
   "index",
   "method",
   "default",
);

我想让第一个键等于第二个键,依此类推

$actions = array(
  "controller" => "index",
  "method" => "default"
);

我怎么能这样做谢谢!!

1 个答案:

答案 0 :(得分:2)

以下是for循环的示例: -

<?php

$actions = array(
   "controller",
   "index",
   "method",
   "default",
);

for($i = 0; $i < count($actions); $i++)
{
   $arr[$actions[$i]] = $actions[$i += 1];
}

echo "<pre>";
print_r($arr);