Jump to content

in_array problem


loquela

Recommended Posts

Hi there I have an array $match_subject:

 

array(2) { [0]=>  object(stdClass)#29 (1) { ["subject_id"]=>  string(2) "17" } [1]=>  object(stdClass)#30 (1) { ["subject_id"]=>  string(2) "21" } }

 

And I want to check for a match so I am using in_array:

 

if (in_array("17", $match_subject){

//do stuff

}

 

But my if is returning a false. Any ideas why? Anyone?

 

Thanks in advance,

 

S.

Link to comment
https://forums.phpfreaks.com/topic/196798-in_array-problem/
Share on other sites

It looks like you have an array of objects, not an array of arrays.  in_array works if by comparing the array of the second parameter directly on the first level.  So if the $match_subject has nested arrays, you'll need to dig deeper to look for the values.  If you post how you've generated the array, that would help.

Link to comment
https://forums.phpfreaks.com/topic/196798-in_array-problem/#findComment-1033107
Share on other sites

Thanks for responding,

 

I'm using codeigniter. $match_subjects is the return from a database query:

 

function topic2subject($data)
{
	$this->db->select('subject_id');
	$query = $this->db->get_where('pp_topic2subject', array('topic_id' => $data['topic_id']));

	if($query->num_rows() > 0) {
		foreach($query->result() as $t2s) {
		$match_subject[] = $t2s;
			}
		return $match_subject;
	}	
}

Link to comment
https://forums.phpfreaks.com/topic/196798-in_array-problem/#findComment-1033117
Share on other sites

In that case, you'll want to put the value of each subject_id into the array directly.

 

If you can access the variable subject_id directly, try this.

$match_subject[] = $t2s->subject_id;

 

If the members are protected/private, then perhaps there's a getter function to get it.

$match_subject[] = $t2s->getSubjectId();

 

I don't know the API, but perhaps that'll help step you to the right direction.

Link to comment
https://forums.phpfreaks.com/topic/196798-in_array-problem/#findComment-1033121
Share on other sites

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.