Match data between two arrays

Discussion in 'PHP' started by Omzy, Feb 12, 2009.

  1. #1
    I have two arrays as such:

    
    $cats=array(
    	'wedding-venues'=>array('1', '2', 'Wedding Venues'),
    	'wedding-caterers'=>array('1', '3', 'Wedding Caterers'),
    );
    
    $subcats=array(
    	'banquet-halls'=>array('wedding-venues', 'Banquet Halls'),
    	'hotels'=>array('wedding-venues', 'Hotels'),
    	'general-caterers'=>array('wedding-caterers', 'Wedding Caterers'),
    );
    
    Code (markup):
    And I have created a foreach loop which is meant to match $subcats to $cats:

    
    foreach($subcats as $index => $value)
    {
    	if($value[0]==array_keys($cats))
    	{
    		//print out the matching values;
    	}
    }
    
    Code (markup):
    But this doesn't seem to work. Anyone got any ideas how to do this? It should just be as simple as that, but I can't figure out why that won't work!
     
    Omzy, Feb 12, 2009 IP
  2. unski84

    unski84 Peon

    Messages:
    133
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Array_keys will create a array of keys, so you are comparing string to array.


    foreach($subcats as $index => $value)
    {
    echo $value[0]."<br>";
    echo array_keys($cats)."<br>";
    if($value[0]==array_keys($cats))
    {
    //print out the matching values;
    }
    }

    This will output:
    wedding-venues
    Array
    wedding-venues
    Array
    wedding-caterers
    Array
     
    unski84, Feb 12, 2009 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So then how do I do it?
     
    Omzy, Feb 12, 2009 IP
  4. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    foreach($subcats as $value)
    {
    	foreach($cats as $donkey){
                        
                     if($value == $donkey){
                             $match[] = value;
                     }
    
            }
            foreach($match as $val){ print $val }
    }
    PHP:
    ??
     
    SiteTalkZone, Feb 12, 2009 IP