如何在foreach循环中创建数组索引?

时间:2016-05-27 06:42:40

标签: php laravel

我有一个数组我想以索引格式使用这个数组。我正在使用foreach

$role_rights = array();
    foreach ($write_read_permission as $k => $val) {

        $role_rights['menu_url'][] = $k;

        $category = explode("/",$k);
        $role_rights['menu_category'][] = $category[0];

        if ('read/write' === $val) {
            $role_rights['read'][] = 1;
            $role_rights['write'][] = 1;                
        }            
        if ('read' === $val) {
            $role_rights['read'][] = 1;
            $role_rights['write'][] = 0;
        }            
        if ('write' === $val) {
            $role_rights['read'][] = 0;
            $role_rights['write'][] = 1;
        }            

    }
循环输出之后

但是我不希望这个输出

 Array
(
    [menu_url] => Array
        (
            [0] => monitoring/tickets
            [1] => monitoring/serach_tickets
        )

    [menu_category] => Array
        (
            [0] => monitoring
            [1] => monitoring
        )

    [read] => Array
        (
            [0] => 1
            [1] => 1
        )

    [write] => Array
        (
            [0] => 1
            [1] => 0
        )
)

我希望在此格式中输出

 Array
(
    0 => Array
        (
            [menu_url] = > monitoring/tickets
            [menu_category] => monitoring
            [read] => 1
            [write] => 0
        )
    1 => Array
        (
            [menu_url] = > monitoring/serach_tickets
            [menu_category] => monitoring
            [read] => 1
            [write] => 1
        )
)

是不可能的。如果可能请帮帮我!!!

4 个答案:

答案 0 :(得分:2)

$role_rights = array();
foreach ($write_read_permission as $k => $val) {
    $rights = [];
    $rights['menu_url'] = $k;

    $category = explode("/",$k);
    $rights['menu_category'] = $category[0];

    if ('read/write' === $val) {
        $rights['read'] = 1;
        $rights['write'] = 1;                
    }            
    if ('read' === $val) {
        $rights['read'] = 1;
        $rights['write'] = 0;
    }            
    if ('write' === $val) {
        $rights['read'] = 0;
        $rights['write'] = 1;
    }       
    array_push($role_rights, $rights);

}

答案 1 :(得分:1)

试试这个:

$mainArray = array('menu_url'=>array('monitoring/tickets','monitoring/serach_tickets'),'menu_category'=>array('monitoring','monitoring'),
    'read'=>array('1','1'),'write'=>array('1','0'));
$finalArr = array();

foreach($mainArray as $key=>$value) {
    $cnt = 0;
    foreach($value as $key_inner=>$subArr) {
        $finalArr[$cnt][$key] = $subArr;
        $cnt++;
    }
}
print '<pre>';print_r($finalArr);

输出:

Array
(
    [0] => Array
        (
            [menu_url] => monitoring/tickets // [menu_url][0]
            [menu_category] => monitoring // [menu_category][0]
            [read] => 1 // [read][0]
            [write] => 1 // [write][0]
        )

    [1] => Array
        ( 
            [menu_url] => monitoring/serach_tickets // [menu_url][1]
            [menu_category] => monitoring // [menu_category][1]
            [read] => 1 // [read][1]
            [write] => 0 // [write][1]
        )

)

答案 2 :(得分:0)

试试这个

$role_rights = array();
$i = 0;
    foreach ($write_read_permission as $k => $val) {

        $role_rights[$i]['menu_url'] = $k;

        $category = explode("/",$k);
        $role_rights[$i]['menu_category'] = $category[0];

        if ('read/write' === $val) {
            $role_rights[$i]['read'] = 1;
            $role_rights[$i]['write'] = 1;                
        }            
        if ('read' === $val) {
            $role_rights[$i]['read'] = 1;
            $role_rights[$i]['write'] = 0;
        }            
        if ('write' === $val) {
            $role_rights[$i]['read'] = 0;
            $role_rights[$i]['write'] = 1;
        }            

        $i++;
    }

print_r($role_rights);

答案 3 :(得分:0)

我不知道php。所以,我的代码可能看起来很傻。只是回答我从问题中的代码中理解的内容。 尝试使用您自己的索引和tmp数组,如下所示:

$index = 0;
$role_rights = array();
foreach ($write_read_permission as $k => $val) {
    $tmp = array();
    $tmp['menu_url'] = $k;
    $category = explode("/",$k);
    $tmp['menu_category'] = $category[0];
    if ('read/write' === $val) {
        $tmp['read'] = 1;
        $tmp['write'] = 1;                
    }            
    if ('read' === $val) {
        $tmp['read'] = 1;
        $tmp['write'] = 0;
    }            
    if ('write' === $val) {
        $tmp['read'] = 0;
        $tmp['write'] = 1;
    }            
    $role_rights[$index] = $tmp;
    $index++;
}