Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

3 arrays, preg_match and merge of the results in PHP

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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 dublicates:

$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. Unfortunatelly I cannot get it zealized. Do you have any idea how to get it? Maybe is any more simple way to realize it?

>Solution :

Loop through the array with all options. Check if the ID is in your $unique list, then it’s available.

foreach ($ct_all_house_options as [$id, $name]) {
    if (in_array($id, $unique)) {
        echo "$id - $name - available<br>";
    } else {
        echo "$id - $name - not available<br>";
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading