Actually I have made small programme in php using simple array and search name from array and my programe given below :
$n = array("Akki","Amesh","Akit","Baa","aaakk");
$hint = "";
$find = "ak";
if ($find !== "") {
$find = strtolower($find);
$lwn = strlen($find);
foreach($n as $gt){
if(stristr($find, substr($gt,0,$lwn))){
if($hint === ''){
$hint .= $gt;
}else{
$hint .= ",$gt";
}
}
}
}
echo ($hint == "") ? "NNN" : $hint ;
My query how to check $hint got data are single & double and how to add comma after got name from array
Like I have searched name using word ak and i got two name Akki and Akit. and its perfect but i want to know how to add comma between that name.
And What does check this condition : ($hint === '')
tell me if anyone know my query.
What this code does:
if($hint === ''){
$hint .= $gt;
}else{
$hint .= ",$gt";
}
is see if $hint is empty (which it will be the first time the preceding if condition is true, since it is initialized to empty at the beginning). This is used to add a comma only if there is already a value in $hint. The === operator simply compares value and type as opposed to only value comparison when using ==. It is really not necessary here.
Does that answer your question?
EDIT: Regarding the comma, as far as I can see the comma is added currently. The code has potential for improvement, but works.
To add comma change your else condition :
else{
$hint .= ","."$gt";
}
your if condition: if($hint === ''){
will check for $hint
's value and datatype as well. if datatype matches then it will check for the value.
so in your case it will check for string
datatype and then compare with $hint
's value i.e. blank string.
Also as mentioned in answer you should use implode
function.
Just to help out with an easier solution:
$hint = implode(", ", preg_grep("/^$find/i", $n));
^
for $find
case-insensitive i
,