Ninjakreborn Posted July 28, 2009 Share Posted July 28, 2009 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? Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 28, 2009 Share Posted July 28, 2009 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; } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 28, 2009 Share Posted July 28, 2009 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? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 28, 2009 Author Share Posted July 28, 2009 This is the only key for now. I am trying to change the array structure to see if it will work then I am going to try foodigi's function as well and see how that goes in just a bit. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 28, 2009 Share Posted July 28, 2009 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)) { } Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 28, 2009 Author Share Posted July 28, 2009 Hmm. I tried that guys original function and it was a good idea. I was worried about performance. Thanks for that modified function. Quote Link to comment 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.