Jump to content

How to know if a value is inside the array?


lopes_andre

Recommended Posts

Hi,

 

I have an array like this one:

Array ( [0] => stdClass Object ( [id_distrito] => 01 ) [1] => stdClass Object ( [id_distrito] => 04 ) [2] => stdClass Object ( [id_distrito] => 11 ) [3] => stdClass Object ( [id_distrito] => 13 ) ) 

 

How can I know if for example the value '11' is inside this array?

 

Best Regards,

The print out does not show an array containing arrays, but rather an array containing objects.  There are lots of ways to do what you want, but a simple one would be:

 

data = array(
(object) array('id_distrito' => '01'),
(object) array('id_distrito' => '04'),
(object) array('id_distrito' => '11'),
(object) array('id_distrito' => '13'),
);

$contains = FALSE;
foreach ($data as $object) {
if ($object->id_distrito === '11') {
	$contains = TRUE;
	break;
}
}

 

If $contains is true after that, the array contains an object with id 11.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.