Jump to content

multidimensional array search/find


samjsharples

Recommended Posts

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')

)

Link to comment
Share on other sites

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 by kierany5
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.