lopes_andre Posted February 20, 2010 Share Posted February 20, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/192750-how-to-know-if-a-value-is-inside-the-array/ Share on other sites More sharing options...
sader Posted February 20, 2010 Share Posted February 20, 2010 if(in_array(11, $array)) { } Quote Link to comment https://forums.phpfreaks.com/topic/192750-how-to-know-if-a-value-is-inside-the-array/#findComment-1015358 Share on other sites More sharing options...
lopes_andre Posted February 20, 2010 Author Share Posted February 20, 2010 Hi, Thanks for the reply. But it does not work. I this this array is an array inside an array. Someone can help. Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/192750-how-to-know-if-a-value-is-inside-the-array/#findComment-1015359 Share on other sites More sharing options...
salathe Posted February 20, 2010 Share Posted February 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192750-how-to-know-if-a-value-is-inside-the-array/#findComment-1015394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.