Multi-Dimensional Array Search by Multiple Conditions

Asked 2 years ago, Updated 2 years ago, 55 views

Ladies and gentlemen,

Thank you for your help.
As the number of searches is not large, I am trying to create a search system with CSV.

You are trying to create a table by retrieving data from form, selecting a province from $value_1 in the check box, selecting a fruit or vegetable from $value_2 in the radio button, searching for a multidimensional array from foreach, and spitting out the appropriate array.

However, the following symptoms have occurred:
Case 1: Search by Aomori prefecture, fruit, etc. 2 No. 2 appears, but No. 6 does not hit in search
Case 2: Search by Saitama Vegetables 検索 Search results are not a hit

Is the logic of the search incorrect?Please give me guidance.

 CSV Data
1. Chiba Prefecture, Fruit, Tangerine
2. Aomori prefecture, fruits, apples
3. Saitama Prefecture, Vegetables, Tomatoes
4, Chiba Prefecture, Vegetables, Broccoli
5, Saitama Prefecture, Vegetables, Spinach
6, Aomori Prefecture, Fruit, Pineapple
7, Saitama Prefecture, Fruit, Peach

[PHP Code]

 echo'<table border="1">tr>th>#2>/th>State>/th>State>>Type</th>th>Name>>>>

$value_1[0] = "Aomori Prefecture";
$value_1[1]="Saitama Prefecture";
$value_2 = "fruit";

echo$value_1[0];

foreach($newarray as$v){ 
    if($v[2]==$value_2){
                if(preg_grep("/".$v[1]."/",$value_1)){ 
                echo "<tr>\n";
                echo "<td align='center'>"."$v[0]."</td>\n";
                echo "<td>".$v[1]."</td>\n"; 
                echo "<td>".$v[2]."</td>\n"; 
                echo "<td>".$v[3]."</td>\n"; 
                echo "</tr>\n";
                $n = $n+1;
                }
          } else {        
        Enter echo'<br> criteria and press Search.';
        break;
    }

}

echo "</table>";

php array

2022-09-30 19:46

1 Answers

Transcribe comments as answer.

if($v[2]==$value_2){...}else{...;break;}, so for loop ends on a line other than the fruit.In other words, the search ends with the line 3, Saitama Prefecture, Vegetables, Tomatoes in CSV data.

else Try rewriting the code below to something other than break as shown below.

<?php
echo'<table border="1"><tr>>#2>/th>>State>/th>Type><th>Name<>/th>>>>
$value_1[0] = "Aomori Prefecture";
$value_1[1]="Saitama Prefecture";
$value_2 = "fruit";
$newarray=array(
  array(1, Chiba prefecture, fruit, tangerine),
  array(2, "Aomori Prefecture", "Fruit", "Apple"),
  array(3, Saitama prefecture, vegetables, apples),
  array(4, Chiba prefecture, vegetables, broccoli),
  array(5, Saitama prefecture, vegetables, spinach),
  array(6, "Aomori Prefecture", "Fruit", "Pineapple"),
  array(7, Saitama prefecture, fruit, peach)
);
echo$value_1[0];
$n = 0;
foreach($newarray as$v){ 
    if($v[2]==$value_2){
        if(preg_grep("/".$v[1]."/",$value_1)){ 
            echo "<tr>\n";
            echo "<td align='center'>"."$v[0]."</td>\n";
            echo "<td>".$v[1]."</td>\n"; 
            echo "<td>".$v[2]."</td>\n"; 
            echo "<td>".$v[3]."</td>\n"; 
            echo "</tr>\n";
            $n = $n+1;
        }
    } else {        
        Enter echo'<br> criteria and press Search.';
        // Some sort of action
        continue;
    }
}
echo "</table>";
?>


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.