HS1993
HS1993

Reputation: 129

Remove key and value from associative array inside of array

I have a little bit chaotic problem... As you can see in code below, I have an array, which contains other arrays. It creates user navigation according to their roles. Navigation items are inherited and here is my problem. I need to set values for key 'dokumenty' in the lowest role EXPERT, than it shouldn't be visible for roles REPORTER and REFERENT and than again visible for EDITOR. So I am sharing EXPERT and REPORTER code, where I tried to remove values for 'dokumenty', but it threw me an error with array_merge()... I will be glad for any help, how to remove row with 'dokumenty' key. I think I shouldn't share whole return, because it's quite long.

     return array(
         AuthRoleEnum::EXPERT => array(
            'prehled' => array('order' => 1, 'href' => ':Dashboard:Default:default', 'label' => 'Prehled', 'ico' => 'fa fa-th-list'),
            'pozemky' => array('order' => 3, 'href' => '#', 'label' => 'Pozemky', 'ico' => 'fa fa-flag-o', 'nodes' => array(
                array('order' => 1, 'href' => ':Land:List:default', 'label' => 'Seznam pozemku', 'ico' => 'fa fa-flag-o'),
                array('order' => 2, 'href' => ':Land:List:selected', 'label' => 'Muj vyber', 'ico' => 'fa fa-flag-o'),
            )),
            'mapa' => array('order' => 4, 'href' => ':Map:Default:default', 'label' => 'Mapa', 'ico' => 'fa fa-globe'),
            'dokumenty' => array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'nodes' => array(
                array('order' => 12, 'href' => ':ExpertReview:ContractsList:default', 'label' => 'Posudky', 'ico' => 'fa fa-folder'),
            )),
        ),
        AuthRoleEnum::REPORTER => array(
            'dokumenty' => null,
            'workflow' => array('order' => 20, 'href' => ':Construction:List:workflow', 'label' => 'Workflow', 'ico' => 'fa fa-cubes'),
        ),
        AuthRoleEnum::REFERENT => array(
            'stavby' => array('order' => 2, 'href' => ':Construction:List:default', 'label' => 'Stavby', 'ico' => 'fa fa-road', 'nodes' => [
                ['order' => 1, 'href' => ':Construction:List:default', 'label' => 'Seznam staveb', 'ico' => 'fa fa-road']
            ]),
            'kontakty' => array('order' => 9, 'href' => ':Contact:List:default', 'label' => 'Kontakty', 'ico' => 'fa fa-user', 'nodes' => []),
            'dokumenty' => array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'nodes' => array(
                array('order' => 9, 'href' => ':Document:List:default', 'hrefArgs' => array('landId' => null), 'label' => 'Seznam dokumentu', 'ico' => 'fa fa-newspaper-o'),
            )),
        ),
        AuthRoleEnum::EDITOR => array(
            'alarmy' => array('order' => 5, 'href' => '#', 'label' => 'Alarmy', 'ico' => 'fa fa-bell-o', 'nodes' => array(
                array('order' => 6, 'href' => ':Alarm:List:default', 'label' => 'Seznam alarmu', 'ico' => 'fa fa-bell-o', 'class' => 'alarm-menu-field')
            )),
            'dokumenty' => array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'nodes' => array(
                array('order' => 10, 'href' => ':InsertionCadastral:GenerateInsertion:list', 'label' => 'Pozadavky na vklad do KN', 'ico' => 'fa fa-cloud-upload'),
                array('order' => 11, 'href' => ':Document:GenerateDocx:firstStep', 'label' => 'Generovani dokumentu', 'ico' => 'fa fa-pencil-square', 'class' => 'show-in-modal'),
                array('order' => 12, 'href' => ':ExpertReview:ContractsList:default', 'label' => 'Posudky', 'ico' => 'fa fa-folder'),
            )),
            'fronta' => array('order' => 15, 'href' => ':Queue:List:default', 'label' => '', 'ico' => 'fa fa-circle', 'class' => 'queue-menu-field'),
            'pozemky' => array('order' => 3, 'href' => '#', 'label' => 'Pozemky', 'ico' => 'fa fa-flag-o', 'nodes' => array(
                array('order' => 3, 'href' => ':Land:Comment:list', 'label' => 'Parcely s poznamkou', 'ico' => 'fa fa-flag-o'),
                array('order' => 4, 'href' => ':BuildingObject:List:default', 'label' => '_T_MAIN_MENU_TITLE_BUILDING_OBJECT', 'ico' => 'fa fa-building')
            )),
        ),
        AuthRoleEnum::ADMIN => array(
            'stavby' => array('order' => 2, 'href' => ':Construction:List:default', 'label' => 'Stavby', 'ico' => 'fa fa-road', 'nodes' => [
                ['order' => 2, 'href' => ':Construction:UserActivity:default', 'label' => 'Aktivita uzivatelu', 'ico' => 'fa fa-user']
            ]),
            'system' => array('order' => 10, 'href' => '#', 'label' => 'System', 'ico' => 'fa fa-cog', 'nodes' => array(
                array('order' => 4, 'href' => ':Setting:UserConstruction:default', 'label' => 'Sprava opravneni uzivatelu a staveb', 'ico' => 'fa fa-building-o'),
                array('href' => ':Setting:User:default', 'label' => 'Uzivatelske ucty', 'ico' => 'fa fa-list')
            )),
            'alarmy' => array('order' => 5, 'href' => '#', 'label' => 'Alarmy', 'ico' => 'fa fa-bell-o', 'nodes' => array(
                array('order' => 7, 'href' => ':Alarm:Edit:add', 'label' => 'Pridani alarmu', 'ico' => 'fa fa-pencil-square', 'class' => 'show-in-modal')
            )),
        ),
        AuthRoleEnum::SUPERADMIN => array(
            'kontakty' => array('order' => 18, 'href' => '#', 'label' => 'Kontakty', 'ico' => 'fa fa-user', 'nodes' => array(
                array('href' => ':Contact:List:default', 'label' => 'Kontakty', 'ico' => 'fa fa-folder'),
                array('href' => ':ExpertReview:Contact:default', 'label' => 'Posudkari', 'ico' => 'fa fa-folder')
            )),
            'system' => array('order' => 11, 'href' => '#', 'label' => 'System', 'ico' => 'fa fa-list', 'nodes' => array(
                array('href' => ':Setting:Selection:default', 'label' => 'Ciselniky', 'ico' => 'fa fa-list'),
                array('href' => ':Setting:System:edit', 'label' => 'Zakladni parametry', 'ico' => 'fa fa-list', 'class' => 'show-in-modal', 'dataredirect' => 'false'),
                array('href' => ':Setting:ConstructionGeneratedSelections:default', 'label' => 'Sablony generovanych dokumentu', 'ico' => 'fa fa-list'),
                array('href' => ':Setting:ConstructionInsertedSelections:default', 'label' => 'Sablony vkladanych dokumentu', 'ico' => 'fa fa-list'),
                array('href' => ':Setting:WsUsers:default', 'label' => 'Uzivatele webovych sluzeb', 'ico' => 'fa fa-credit-card'),
                array('href' => ':Setting:UserLog:default', 'label' => 'Kompletni log', 'ico' => 'fa fa-h-square'),
                array('href' => 'changeDebugMode!', 'label' => $debugModeLabel, 'ico' => 'fa fa-gears'),
                array('href' => ':Setting:Changelog:list', 'label' => 'Changelog', 'ico' => 'fa fa-gears'),
            )),
        )
    );

Error while I try 'dokumenty' => null,:

array_merge(): Argument #1 is not an array search

Upvotes: 0

Views: 135

Answers (2)

HS1993
HS1993

Reputation: 129

Solved, I had to use shorthand for if and it works.

 'dokumenty' => (($userRole[0] == "EXPERT") ? (array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'hidden' => 0, 'nodes' => array(
      array('order' => 12, 'href' => ':ExpertReview:ContractsList:default', 'label' => 'Posudky', 'ico' => 'fa fa-folder'),
      ))) : null),
  ),

Upvotes: 1

Road2PreSchool
Road2PreSchool

Reputation: 80

If i understood you right than you intent to remove the Dokumentry key from the array which can be achieved with the unset method unset($arrayName['keyName']);

If you just want to unset the value of Dokumentry in that array than you should do this:

 $yourArray = array(
         AuthRoleEnum::EXPERT => array(
            'prehled' => array('order' => 1, 'href' => ':Dashboard:Default:default', 'label' => 'Prehled', 'ico' => 'fa fa-th-list'),
            'pozemky' => array('order' => 3, 'href' => '#', 'label' => 'Pozemky', 'ico' => 'fa fa-flag-o', 'nodes' => array(
                array('order' => 1, 'href' => ':Land:List:default', 'label' => 'Seznam pozemku', 'ico' => 'fa fa-flag-o'),
                array('order' => 2, 'href' => ':Land:List:selected', 'label' => 'Muj vyber', 'ico' => 'fa fa-flag-o'),
            )),
            'mapa' => array('order' => 4, 'href' => ':Map:Default:default', 'label' => 'Mapa', 'ico' => 'fa fa-globe'),
            'dokumenty' => array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'nodes' => array(
                array('order' => 12, 'href' => ':ExpertReview:ContractsList:default', 'label' => 'Posudky', 'ico' => 'fa fa-folder'),
            )),
        ),
        AuthRoleEnum::REPORTER => array(
            'dokumenty' => null,
            'workflow' => array('order' => 20, 'href' => ':Construction:List:workflow', 'label' => 'Workflow', 'ico' => 'fa fa-cubes'),
        ));
unset($yourArray[AuthRoleEnum::REPORTER]['dokumenty'] );
return $yourArray;

Or maybe your problem would be solved by using null instead of using qoutes ''.

return array(
     AuthRoleEnum::EXPERT => array(
        'prehled' => array('order' => 1, 'href' => ':Dashboard:Default:default', 'label' => 'Prehled', 'ico' => 'fa fa-th-list'),
        'pozemky' => array('order' => 3, 'href' => '#', 'label' => 'Pozemky', 'ico' => 'fa fa-flag-o', 'nodes' => array(
            array('order' => 1, 'href' => ':Land:List:default', 'label' => 'Seznam pozemku', 'ico' => 'fa fa-flag-o'),
            array('order' => 2, 'href' => ':Land:List:selected', 'label' => 'Muj vyber', 'ico' => 'fa fa-flag-o'),
        )),
        'mapa' => array('order' => 4, 'href' => ':Map:Default:default', 'label' => 'Mapa', 'ico' => 'fa fa-globe'),
        'dokumenty' => array('order' => 8, 'href' => '#', 'label' => 'Dokumenty', 'ico' => 'fa fa-newspaper-o', 'nodes' => array(
            array('order' => 12, 'href' => ':ExpertReview:ContractsList:default', 'label' => 'Posudky', 'ico' => 'fa fa-folder'),
        )),
    ),
    AuthRoleEnum::REPORTER => array(
        'dokumenty' => null,
        'workflow' => array('order' => 20, 'href' => ':Construction:List:workflow', 'label' => 'Workflow', 'ico' => 'fa fa-cubes'),
    ),

If this doesn't help you, please provide the entire return, error message and try to define what you are trying to do more precisely.

The same error has been explained in this question Warning: array_merge(): Argument #1 is not an array, when processing two $_POST

Upvotes: 0

Related Questions