所以我的程序有问题,我在一个名为ArrName的数组中搜索名称然后显示英俊或美丽如果他/她存在于数组中但如果名称不在数组中那么程序将显示相关如果我搜索的字符串在数组中不存在,则为字符串。我很难解决这个请求帮助。这是我的榜样。
示例输出#1:
姓名: Bob,John,Charlie,Bravo,Alpha,Raymond,Kenneth,Rose,Rosel,James
搜索: bob。
结果:鲍勃存在,鲍勃很帅
示例输出#2:
姓名: Bob,John,Charlie,Bravo,Alpha,Raymond,Kenneth,Rose,Rosel,James
搜索: roger。
结果:找不到Roger。你的意思是:
玫瑰?
罗塞尔?
到目前为止,这是我的代码:
<html>
<body>
<?php
$arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
$arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
$arrgirls = array('rosel','rosil','rose','joy','juy');
$strsearch = 'rosel';
$count = 0;
$badd = 0;
$gadd = 0;
foreach($arrNumbers as $value)// checks if name exists
{
if($value == $strsearch)
{
$count = $count + 1;
}
else
{
$count = $count + 0;
}
}
if($count > 0)// if name exists, checks if the name is from a boy or a girl
{
//boys
foreach($arrboys as $value)// checks if the name is from the boys
{
if($value == $strsearch)
{
$badd = $badd + 1;
}
else
{
$badd = $badd + 0;
}
}
if($badd > 0)
{
echo $strsearch.' is handsome';
}
// girls
foreach($arrgirls as $value)// checks if the name is from the girls
{
if($value == $strsearch)
{
$gadd = $gadd + 1;
}
else
{
$gadd = $gadd + 0;
}
}
if($gadd > 0)
{
echo $strsearch.' is beautiful';
}
}
else// if the name does not exists
{
// this is the part where i dont know what to do.
// this part suppose to display all the names related to the name that is
// being searched.
echo 'did you mean';
}
?>
</body>
答案 0 :(得分:0)
为什么要在$ arrNumbers数组中检查$ arrboys和$ arrgirls是$ arrNumbers的子串。
使用以下代码
<?php
$arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
$arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
$arrgirls = array('rosel','rosil','rose','joy','juy');
$strsearch = 'rosel';
$flag = '';
for($i=0;$i<sizeof($arrboys);$i++)
{
//echo $arrboys[$i].' ';
if($strsearch == $arrboys[$i]);
$flag = 'boy';
}
for($i=0;$i<sizeof($arrgirls);$i++)
{
//echo $arrboys[$i].' ';
if($strsearch == $arrgirls[$i]);
$flag = 'girl';
}
if($flag == 'boy')
{
echo $strsearch.' exist, '. $strsearch.' is handsome';
}
if($flag == 'girl')
{
echo $strsearch.' exist, '. $strsearch.' is beautiful';
}
else
{
echo $strsearch.' not found. did you mean:';
}
?>