2017-08-30 54 views
-1

我有一个这样的数组:PHP指数数组关联第一元素等于第二等

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

我想要得到的第一个关键等于第二键等等这样

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

我该如何做到这一点,谢谢!

+0

在[所以]你应该尝试**自己写代码**。后** [做更多的研究](//meta.stackoverflow.com/questions/261592)**如果你有问题,你可以**发布你已经尝试**与清楚的解释是什么是'工作**并提供[** Minimal,Complete和Verifiable示例**](// stackoverflow.com/help/mcve)。我建议阅读[问]一个好问题和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要参加[游览]并阅读[this](// meta.stackoverflow.com/questions/347937/)**。 –

+1

循环遍历你的数组使用for循环在这里检查循环http://php.net/manual/en/control-structures.for.php –

回答

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); 
-1

你可以这样做

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

做某事如果您确信包含偶数个元素。

相关问题