我的学习案例有问题。在从table data
获取数组值的同时,我尝试将数组值与string
中的列号进行匹配。因此,如果找到的当前值在列号过程中一个匹配,则可能会完成对最后一个条件的当前数组的处理,如果找到的当前数组值与列号过程中的一个匹配,则将在当前数组值之后追加对所找到的当前值的处理。在列号中找不到的值将是它的数组值。有关更多详细信息,让我们看看
否-数字-c_num
1-1-1A2B-XXXX
2-1-1A2B 3C4D-YYYY
3-1A2B 3C4D 5E6F-ZZZZ
<?php
$result = "";
$string = "1A2B 1A2B 3C4D 1A2B 1A2B 3C4D 1A2B 3C4D 5E6F 7G8H";
$array = explode(" ", $string);
foreach($array as $key => $value){
$query = "SELECT * FROM WHERE number LIKE '$value%'";
$exe = $connect->query($query);
if($exe->num_rows > 1){
//
//do something?
//
} else if(empty($exe->num_rows)) {
$result .= $value." ";
} else {
$row = $exe->fetch_array();
$result .= $row['c_num']." ";
}
}
?>
XXXX YYYY XXXX YYYY ZZZZ 7G8H
array(
[XXXX] => 1A2B
[YYYY] => 1A2B 3C4D
[XXXX] => 1A2B
[YYYY] => 1A2B 3C4D
[ZZZZ] => 1A2B 3C4D 5E6F
[7G8H] => 7G8H
);
请给我一个建议:)
答案 0 :(得分:1)
您可能想先将所有数据库获取到一个数组中,并创建一个“映射”,然后遍历它。
/* Query */
$result = array(
array('number' => '1A2B', 'c_num' => 'XXXX'),
array('number' => '1A2B 3C4D', 'c_num' => 'YYYY'),
array('number' => '1A2B 3C4D 5E6F', 'c_num' => 'ZZZZ'),
);
//$result = mysql_query("SELECT number, c_num FROM table");
/* Create Map */
$map = array();
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($result as $row) {
$numbers = explode(" ", $row['number']);
$value = $row['c_num'];
$count = count($numbers);
$maps = array();
create_map($maps, 0, $numbers, $count, $value);
$map = array_merge_recursive($map, $maps);
}
function create_map(&$map, $i = 0, $numbers, $count, $value)
{
$map = array($numbers[$i] => array());
if ($i == $count-1)
$map[$numbers[$i]]['value'] = $value;
if ($i < $count-1)
create_map($map[$numbers[$i++]], $i, $numbers, $count, $value);
}
/* Search */
$string = "1A2B 1A2B 3C4D 1A2B 1A2B 3C4D 1A2B 3C4D 5E6F 7G8H";
$keys = explode(" ", $string);
$tmp_map = $map;
for ($i = 0; $i < count($keys); $i++) {
$key = $keys[$i];
$next_key = isset($keys[$i+1]) ? $keys[$i+1] : null;
$tmp_map = &$tmp_map[$key];
if (!isset($tmp_map[$next_key])) {
//echo $key;
echo isset($tmp_map['value']) ? $tmp_map['value'] : $key; echo " ";
$tmp_map = $map;
}
}
编辑:如果搜索字符串不是那么大,则可以通过选择包含搜索字符串中数字的记录来进行优化。
/* Query */
$search_string = "1A2B 1A2B 3C4D 1A2B 1A2B 3C4D 1A2B 3C4D 5E6F 7G8H";
$unique_ss = array_unique(explode(" ", $search_string));
$search_query = "number LIKE '". implode("*' OR number LIKE '", $unique_ss) . "*'";
$result = mysqli_query("SELECT number, c_num FROM table WHERE {$search_query}");
/* Create Map */
$map = array();
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$numbers = explode(" ", $row['number']);
$value = $row['c_num'];
$count = count($numbers);
$maps = array();
create_map($maps, 0, $numbers, $count, $value);
$map = array_merge_recursive($map, $maps);
}
function create_map(&$map, $i = 0, $numbers, $count, $value)
{
$map = array($numbers[$i] => array());
if ($i == $count-1)
$map[$numbers[$i]]['value'] = $value;
if ($i < $count-1)
create_map($map[$numbers[$i++]], $i, $numbers, $count, $value);
}
/* Search */
$keys = explode(" ", $search_string);
$tmp_map = $map;
for ($i = 0; $i < count($keys); $i++) {
$key = $keys[$i];
$next_key = isset($keys[$i+1]) ? $keys[$i+1] : null;
$tmp_map = &$tmp_map[$key];
if (!isset($tmp_map[$next_key])) {
//echo $key;
echo isset($tmp_map['value']) ? $tmp_map['value'] : $key; echo " ";
$tmp_map = $map;
}
}