1

Im using codeigniter, Yes i have searched internet and i have in_array() but it seems that it is not working.

See here is the array i am getting from database.

Array ( [0] => stdClass Object ( [FormCIPath] => admin/dashboard/System ) [1] => stdClass Object ( [FormCIPath] => admin/dashboard/Users ) [2] => stdClass Object ( [FormCIPath] => admin/residentials/# ) [3] => stdClass Object ( [FormCIPath] => admin/configurations/# ) [4] => stdClass Object ( [FormCIPath] => admin/configurations/ManageForms ) [5] => stdClass Object ( [FormCIPath] => admin/residentials/Houses ) [6] => stdClass Object ( [FormCIPath] => admin/residentials/Flats ) [7] => stdClass Object ( [FormCIPath] => admin/configurations/ManageTabs ) [8] => stdClass Object ( [FormCIPath] => admin/configurations/SitePreferences ) [9] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/# ) [10] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/CreateUser ) [11] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/ListUsers ) ) 

i want to find if configurations/ManageForms is present inside the array, so i tried like this.

$partialURI = $class."/".$method;
if(in_array($partialURI,$result)){
    return "True";
}
else{
    return "FALSE";
}

but i always get False in return. i haved checked the variable $partialURI it is returning configurations/ManageForms. But still i am getting FALSE in return and where as you can see this text is present inside array above??

2
  • why cant you use in_array() ? Commented Oct 6, 2014 at 3:06
  • @AvinashBabu its an object array, thats why i guess in_array() didn't worked for me. Commented Oct 6, 2014 at 3:08

2 Answers 2

2

Since in_array() works only on flat, you could just use a simple foreach loop:

$partialURI = $class."/".$method;
foreach($result as $r) {
    if(stripos($r->FormCIPath, $partialURI) !== false) {
        return 'true';
    }
}
return 'false';
Sign up to request clarification or add additional context in comments.

4 Comments

It looks to be an object array (if I'm correct) this might not work?
@DarylGill since each copy of the array inside is an object $r->FormCIPath just use the arrow operator, and just use stripos since your comparing configurations/ManageForms == admin/configurations/ManageForms
There we go :) forced a slight explanation out of you as to the logic behind your resolution
@DarylGill lol! no i don't feel forced actually glad i could clarify more
1

Try this:

$data= new array ();
foreach ($result as $r) {

$data[]=$r;

}

Now try your code by replacing $result array with $data

$partialURI = $class."/".$method;
    if(in_array($partialURI,$data)){
                return "True";
            }
            else{
                return "FALSE";
            }

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.