Forum Moderators: coopster

Message Too Old, No Replies

Compare arrays with nested foreach

         

leetee4000

8:48 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



I have 3 arrays below:

[PHP]
$designer_arr = array('prada', 'gucci', 'DKNY');

$item_arr = Array(
[prada] => Array('handbag', 'belt'),
[gucci] => Array('hat', 'belt')'
[DKNY] => Array('handbag','hat')
)

$available_arr[] = array($record['designer'],$record['item']);
PHP]

I want to display the contents of the $designer_arr and $item_arr in a table as below:

¦ prada ¦ gucci ¦ dkny ¦
-------------------------
¦ handbag ¦ hat ¦handbag
¦ belt ¦ belt ¦ hat

I then want to compare these results with the contents of the $available_arr. If whats in the results above matches whats in the $available_array I want to colour it green, if not, red. I have the below code which almost works but it brings out the results multiple times. Obviously this is because Im using a foreach to go through the $available_arr to compare against the results, but I dont know how else to compare the two arrays and just diplay one set of data.

[PHP]
foreach($designer_arr as $designer){
foreach ($item_arr[$designer] as $item){
if ($available_numrows != 0){
foreach ($available_arr as $available){
$available_desginer = $available[0];
$available_item = $available[1];
if (($designer == $available_desginer)&&($item == $available_item)){
$class="color:green";
} else {
$class="color:red";
}
$html .= "<span style=\"$class\">$item¦</span>";
}

}
}

}[/PHP]

cameraman

2:32 am on Oct 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, leetee4000.
Have a look at in_array() [us.php.net]
Also look at its 'parent' page, array functions - there's some really good stuff there that may make your task even easier than in_array will, possibly array_intersect or array_intersect_assoc.