I have 3 different multidimensional arrays:
// INPUT DATA WITH HOUSE DESCRIPTION. STRUCTURE: ID, OPTION DESCRIPTION
$input_house_data = array (
array("AAA","Chimney with red bricks"),
array("BBB","Two wide windows in the main floor"),
array("CCC","Roof tiles renewed in 2015")
);
// CATALOGUE WITH ALL HOUSE EQUIPMENT OPTIONS. STRUCTURE: ID, OPTION NAME
$ct_all_house_options = array (
array("0001","Chimney"),
array("0002","Garden"),
array("0003","Roof tiles"),
array("0004","Windows"),
array("0005","Garage")
);
// SEARCH STRINGS AS REGULAR EXPRESSIONS. STRUCTURE: ID, EQUIPMENT OPTION NAME, REGULAR EXPRESSION TO SEARCH
$ct_house_options = array (
array("0001","Chimney","/^Chimney with./"),
array("0003","Roof tiles","/^Roof tiles./"),
array("0004","Windows","/.windows./"),
array("0004","Windows","/.wide windows./")
);
I would like to search within $input_house_data by use of the regular expressions from the array $ct_house_options in order to indicate which equipment has a house. The result should be the whole list with all possible options and status "available" or "not available":
0001 - Chimney - available
0002 - Garden - not available
0003 - Roof tiles - available
0004 - Windows - available
0005 - Garage - not available
I tried to realize it as following:
$arrlength_input_house_data = count($input_house_data);
$arrlength_ct_all_house_options = count($ct_all_house_options);
$arrlength_ct_house_options = count($ct_house_options);
For loop with preg_match function. All results are written into array $matches (including dublicates):
for ($row1 = 0; $row1 < $arrlength_input_house_data; $row1++) {
for ($row2 = 0; $row2 < $arrlength_ct_house_options; $row2++) {
if (preg_match($ct_house_options[$row2][2], $input_house_data[$row1][1]) === 1) {
$matches[] = $ct_house_options [$row2][0];
}
}
}
Deleting of duplicates:
$unique = array_unique($matches);
print_r($unique);
So now I have got the unique results:
Array ( [0] => 0001 [1] => 0004 [3] => 0003 )
The next step should be merge of the array $ct_all_house_options and unique results from $unique. Unfortunately, I cannot get it realized. Do you have any idea how to get it? Maybe there is a more simple way to realize it?
12.08.2022
Hello dear all! thank you for your feedbacks. I checked and tested all of of them. Inbetween the business logic has been changed and became a little bit complicated:
1.There are 3 different constelations to indicate a product option.
- Only by regular expression within product description,
- By regular expression within description + product family or parts of product family,
- By regular expression within description + product family + product number.
2.The output can be different: TRUE/FALSE or specific string (e. g. color of the product "white", "green" etc.).
So please take a look how I designed a possible solution:
$input_product_data = array (
array("AAAA", "9999", "Chimney with red bricks"),
array("CCCC","2234","Two wide windows in the main floor"),
array("RRRR","0022","Roof tiles renewed in 2015"),
array("","2258","Floor has been renovated for two years. Currently it has ground in wood."),
array("","","Beautiful door in green color")
);
// CUSTOMIZING TABLE FOR PRODUCT OPTIONS. STRUCTURE: ID[0], OPTION NAME[1], OPTION CATEGORY[2], OPTION-FAMILY[3], PROD.-NR[4], REG. EXPRESSION[5], PRIORITY[6], OUTPUT[7]
$ct_product_options = array (
array("0001", "Chimney", "Additional options", "/^AAAA/", "9999", "/^Chimney with./", "0", "TRUE"),
array("0003", "Roof tiles", "Basic options", "/^RRRR/", "0022", "/^Roof tiles./", "0", "TRUE"),
array("0004", "Windows", "Basic options", "/^C...$/", "2234", "/.windows./", "0", "TRUE"),
array("0004", "Windows", "Basic options", "/^C...$/", "2567", "/.wide windows./", "0", "TRUE"),
array("0002", "Material of ground floor", "Additional options", "", "/^2.../", "/.wood./", "0", "Wood"),
array("0005", "Door color", "Basic options", "", "", "/.green./", "0", "Green")
);
// IMPORTANT: THE REG. EXPRESSIONS CAN BE DEFINED MANY TIMES (e. g. 10 DIFFERENT REG. EXPRESSIONS FOR OPTION "WINDOWS"). POINTS "." REPRESENTS EMPTY SPACES WITHIN PRODUCT DESCRIPTION AND ARE IMPORTANT TO IDENTIFY EXACTLY AN OPTION.
// FOR LOOP TO MAKE COMPARISON BETWEEN INPUT PRODUCT DATA AND PREDEFINED CUST. STRINGS
$matches_array = array();
foreach ($input_product_data as [$product_family, $product_number, $product_description]) {
foreach($ct_product_options as [$option_id, $option_name, $option_category, $product_family_reg_exp, $product_number_reg_exp, $regular_expression, $priority, $output]) {
if (preg_match($regular_expression, $product_description) == 1
&& preg_match($product_family_reg_exp, $product_family) == 1 ||
preg_match($regular_expression, $product_description) == 1
&& preg_match($product_number_reg_exp, $product_number) == 1) {
$matches_array [] = array("id" => $option_id, "option_name" => $option_name, "option_category" => $option_category, "output"=> $output);
}
else {
if (empty($product_family) && empty($product_number)) {
if (preg_match($regular_expression, $product_description) == 1) {
$matches_array [] = array("id" => $option_id, "option_name" => $option_name, "option_category" => $option_category, "output"=> $output);
}
}
}
}
}
echo "<pre>";
print_r($matches_array);
// FUNCTION FOR DELETE DUBLICATES FROM ARRAY WITH MATCHES
function unique_multidimensional_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
echo "<br><h3>UNIQUE MATCHES</h3>";
// CALL OF THE FUNCTION TO GET UNIQUE MATCHES
$unique_matches = unique_multidimensional_array($matches_array, 'id');
sort($unique_matches);
echo "<pre>";
print_r($unique_matches);
// CALL OF THE FUNCTION TO CREATE LIST/ARRAY WITH ALL AVAILABLE PRODUCT OPTIONS
$list_all_product_options = unique_multidimensional_array($ct_product_options, 0);
$list_all_product_options_short = array();
foreach ($list_all_product_options as $option_item) {
$list_all_product_options_short[] = array("option_id" => $option_item[0], "option_name" => $option_item[1], "option_category" => $option_item[2]);
}
sort($list_all_product_options_short);
echo "<h3>LIST WITH ALL PRODUCT OPTIONS (SHORT VERSION)</h3>";
echo "<pre>";
print_r($list_all_product_options_short);
My questions:
1.How to get crossed two arrays $list_all_product_options_short and $unique_matches within an array in the following format: ALL values from $list_all_product_options_short and only field "output" from $unique_matches?
// EXAMPLE:
0001 - Chimney - Additional options - TRUE
0005 - Door color - Basic options - Green
// etc.
2.Additionally within indication process should be considered new parameter "Priority". It should be used to rewrite/to prioritise results for certain cases. E.g. when we have got two different colors for door "green" (priority = "0") and "red" (priority ="1"), door should get "Output" = "red".
3.For some hints regarding better coding for better performance I would be very appreciate.