samjsharples Posted March 4, 2015 Share Posted March 4, 2015 Hi. I have a list of products and each have parts which in turn have their own codes. I need to find the correct part when I have the code. So in the example below, I already have the code '8R195'. I want to check through all the arrays and find which one contains this code, so the output would be A in this case. Any help greatly appreciated $item1 => Array ( ('A') => array('825R15', '825R16', '65R17', '21575R175', '8R195'), ('B') => array('10R225', '825x20', '900R20'), ('C') => array('15R225', '29575R225', '30570R225', '38565R225') ) Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/ Share on other sites More sharing options...
kierany5 Posted March 4, 2015 Share Posted March 4, 2015 (edited) function yourSearch($needle, $haystack) { foreach($haystack as $i => $v) { if(array_search($needle, $v) !== false) return $i; } return false; } Something like the above? So, echo yourSearch('8R195', $item1); should give A Edited March 4, 2015 by kierany5 Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/#findComment-1507508 Share on other sites More sharing options...
samjsharples Posted March 5, 2015 Author Share Posted March 5, 2015 super duper thank you man Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/#findComment-1507604 Share on other sites More sharing options...
samjsharples Posted March 6, 2015 Author Share Posted March 6, 2015 How could I do this to show multiple results? Say if array A&B both contain the text I am looking for? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/#findComment-1507776 Share on other sites More sharing options...
cyberRobot Posted March 6, 2015 Share Posted March 6, 2015 How could I do this to show multiple results? Say if array A&B both contain the text I am looking for? Any ideas? Instead of returning $i, you could store it in an array. Then just return the array after the foreach loop is complete. Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/#findComment-1507781 Share on other sites More sharing options...
samjsharples Posted March 6, 2015 Author Share Posted March 6, 2015 Thanks cyber Quote Link to comment https://forums.phpfreaks.com/topic/295093-multidimensional-array-searchfind/#findComment-1507782 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.