I'm working on a custom shopping cart variation code and want to check if the selected option array is available out of a main array of all the products options.
For example consider the following as my main array where c1, c2 are the option categories (size, colour, etc) and the value is the id of the option in that category.
$optionsarray = Array
(
[0] => Array
(
[c1] => 70
[c2] => 79
)
[1] => Array
(
[c1] => 71
[c2] => 79
)
[2] => Array
(
[c1] => 71
[c2] => 80
)
[3] => Array
(
[c1] => 70
[c2] => 62
)
[4] => Array
(
[c1] => 71
[c2] => 62
)
);
I want to be able to check if different values exist or not
So if my $selected_array to test is
$selected_array = Array
(
[c1] => 70
)
I'd like to know that
[2] => Array
(
[c2] => 80
)
from my $optionsarray is the only one that has no match for [c2] so I can disable that option on the front end.
The closest I've come to getting this to work is to do the opposite and find the "allowed" matches using the following function ($the_selected_cat in the following example is 1 coming from the front end)
$filteredoptionsarray = array();
$fo = 0;
foreach ($optionsarray as $option) {
$fo ++;
$catvar = "";
$results = array_diff_assoc($option, $selected_array);
$catvar = 'c' . $the_selected_cat;
if (!$results[$catvar]) {
foreach ($results as $result => $value) {
$filteredoptionsarray[] = (array("cat" => $result, "id" => $value));
}
}
}
This gives me the id's that match [c1] => 70 but what I really need is a different function or another step to get the opposite and to spit out the id of what is not available. In this example [c2] =>80
I've attempted some functions with array_intersect_assoc to no avail.
In theory I'm trying to keep this as dynamic as possible so there may be 1, 2, 3 etc option categories.
Any push in the right direction would be greatly appreciated.
<=============== EDIT ===============>
Could be pretty specific to my application but this does the trick now
$the_selected_term = 70;
$the_selected_cat = 1
//above two vars coming from the front-end
$allowed = array();
$disallowed = array();
foreach ( $optionsarray as $option => $value ) {
if( $value['c'.$the_selected_cat] == $the_selected_term ){
for ( $cc = 1; $cc <= $count_cats; $cc++ ) {
if($cc != $the_selected_cat){
$allowed[] = "c".$cc."-".$value['c'.$cc];
}
}
} else {
for ( $cc = 1; $cc <= $count_cats; $cc++ ) {
if($cc != $the_selected_cat){
$disallowed[] = "c".$cc."-".$value['c'.$cc];
}
}
}
}
$disabled = array();
$results=array_diff($disallowed,$allowed);
$results = array_unique($results);