I have a multidimensional array with which I want to search for values and keys using this
<input type="text" onkeyup="showHint(this.value)"></input>
along with this
function showHint(str)
{
if (str.length==0)
{
document.getElementById("nav").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("nav").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
I am just a bit stuck on the php bit. The array looks something like this (shortened)
Array
(
[Modest Mouse] => Array
(
[The Moon & Antarctica] => Array
(
[0] => 3rd Planet
[1] => Gravity Rides Everything
[2] => Dark Centre of the Universe
)
[The Lonesome Crowded West] => Array
(
[7] => Cowboy Dan
[8] => Trailer Trash
[9] => Out of Gas
)
[The Vasco Era] => Array
[Lucille] => Array
(
[0] => Not Stuck Here
[1] => For No One
)
)
)
I start by getting the query
$q=$_GET["q"];
Then I can do this
if (strlen($q) > 0)
{
$hint="";
foreach($a as $b => $c)
{
if (strtolower($q)==strtolower(substr($b,0,strlen($q))))
{
if ($hint=="")
{
$hint=$b;
}
else
{
$hint=$hint." , ".$b;
}
}
}
}
Which can get me Modest Mouse or The Vasco Era, but not anything deeper. If I were to type 'T' into the input field, I would like to be able to get the results 'The Moon & Antartica', 'The Lonesome Crowded West', 'Trailer Trash' and 'The Vasco Era'.