-1

I have this chunk of PHP code which is giving me the error:

Warning: Illegal string offset 'mod_modulecode' in C:\xampp\htdocs\MP\multi\functions.php on line 29

This is the code that the warning is relating to:

function set_rights($menus, $menuRights) {
    $data = array();
    for ($i = 0, $c = count($menus); $i < $c; $i++) {
        $row = array();
        for ($j = 0, $c2 = count($menuRights); $j < $c2; $j++) {
            if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
                if (authorize($menuRights[$j]["rr_create"]) || authorize($menuRights[$j]["rr_edit"]) ||
                        authorize($menuRights[$j]["rr_delete"]) || authorize($menuRights[$j]["rr_view"])
                ) {...................'

Any help would be greatly appreciated.

maxhb
  • 8,554
  • 9
  • 29
  • 53
  • Does your `$menus` array contains `mod_modulecode` key? If it contains, then your loop is trying to access the next element to the last one which doesn't exists. If so, in your loop `$i < $c - 1` try this – Haridarshan Feb 09 '16 at 06:59
  • can you print your array before using it in loop, i doubt that your array keys is not quoted as string. – Amit Shah Feb 09 '16 at 07:01
  • It appears that `$menus` is a string array instead of a multi dimensional array. – SOFe Feb 09 '16 at 07:02
  • $menus[$i] is a string, not an array, and as such, has only numeric offsets. – hanshenrik Feb 09 '16 at 07:04

1 Answers1

0

What PHP is saying is that at some iteration in

if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {

$menus[$i] is a string, not an array, and you're trying to access it like an array containing the key mod_modulecode.

To help track down this bug, I'd suggest:

if(is_string($menus[$i]) {
    var_dump('string bug','i',$i,'j',$j,'$menus[$i]',$menus[$i]);
}
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {

Or even better yet, if you have xdebug installed, which will show local variables when an uncaught exception is thrown: throw new Exception('string bug');

hanshenrik
  • 19,904
  • 4
  • 43
  • 89