简体   繁体   中英

Codeigniter array value check

I have an array like these

$data=array(
'a'=>'value1',
'b'=>'value2',
'c'=>'value3',
'd'=>array('e'=>'value4','f'=>'value5' ),

);

By using CI how to get the value of 'e' and how to check 'e' is equal to any value or not.

This isn't related to CodeIgniter.

You can just do this: $data['d']['e']

And then to check if it's equal to any value do this:

if ($data['d']['e'] == $anyValue) {
    // do something
}

You can get the value as in the case of a two dimensional array..$data['d'] will select the array inside.Then get the value of 'e' or 'f' as $data['d']['e'] or $data['d']['f'] . If you want to compare try:

if ($data['d']['e'] == $Value) {
 //put your code here.....
}

You can use

echo "<pre>";
print_r($data['d']['e']);
die();

inside your code to check what value you have inside the index 'e'. Always use this technique. Very handy.

By the way, this is standard/raw PHP technique, not CI. You can use raw PHP in CI, there's nothing wrong with that.

Checking whether the value you have inside index 'e' is equal to a specific value, is a very basic thing you probably might've learned back in high school or grad school. It's by using an if() statement that you can compare your 'e' value with the specific value you have.

if($data['d']['e'] == 'somevalue')
{
    //do your work here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM