Jump to content

[SOLVED] in_array not working...


Ninjakreborn

Recommended Posts

I have an array.

$array[1]['primary_domain'] = 'value1';

$array[2]['primary_domain'] = 'value2';

$array[3]['primary_domain'] = 'value3';

$array[4]['primary_domain'] = 'value4';

$array[5]['primary_domain'] = 'value5';

$array[6]['primary_domain'] = 'value6';

$array[7]['primary_domain'] = 'value7';

$array[8]['primary_domain'] = 'value8';

$array[9]['primary_domain'] = 'value9';

$array[10]['primary_domain'] = 'value10';

 

Then I have a standard string...might be...

variable = 'value5';

I need to simply do

if (!in_array($variable, $array)) {
// add variable into array if it's not there.
}

 

So far this has been working perfectly. But recently there is one "item" that isn't working property...and I am wondering why.  Is this standard behavior suppose to work or is there something wrong in my implementation?

Link to comment
Share on other sites

i have had issues with in_array and multidimensional arrays... i use this function instead...

 

this is a very simple function and will only support 2 dimensions

 

function in_marray($needle, $haystack)
{
foreach($haystack as $x)
{
	if(in_array($needle,$x))
		return true;
}
return false;
}

Link to comment
Share on other sites

when using $array with in_array() it looks at just the values of $array...which is 10 different arrays. it doesn't look any deeper then that. in each item of $array, you have another array. That array has an item with a key of 'primary_domain' in it...are there other keys or is this the only one?

Link to comment
Share on other sites

the problem with the in_marray() is it can be a waste of resources. as long as your arrays stay small it's fine...you may want to modify it a bit for your case though:

function my_in_array($key, $needle, $haystack)
{
  foreach($haystack as $item)
  {
    if($item[$key] == $needle)
      return true;
  }
  return false;
}

if (!my_in_array('primary_domain', $variable, $array)) {

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.