0

I have one array i want to use this array in index format. I am using 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;
        }            

    }

after looping output like this but i don't want this out put

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

I want output in this formart

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

Is it posssible. If possible please help me!!!

4 Answers 4

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. You should accept it as an answer if it solved your problem. :)
1

Try this:

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

Output:

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]
        )

)

Comments

0

Try this

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

Comments

0

I don't know php. So, my code may look silly. Just answering from what I understood from your code in the question. Try using your own index and tmp array like this:

$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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.