This code, which I've found somewhere online, works fine and it's kind of what I'm trying to accomplish:
$(document).ready(function(){
var arrData = [ "j", "Q", "u", "e","r","y" ];
alert(jQuery.inArray("Q", arrData));
});
However, I have an array from a loop with php/mysql that I output and save like this:
$query = mysql_query("SELECT * FROM geo_orter");
while(($row = mysql_fetch_assoc($query))){
$i = $row['ort_id'];
$result[$i] = $row['ortnamn'];
};
$allaOrterjson=json_encode($result);
Then I go ahead and do this, which works:
var allaOrter=<?php echo $allaOrterjson ?>;
document.write(allaOrter[0] + allaOrter[1] + allaOrter[2]);
and gives me
undefinedAborremålaAbbjörnahall
Here's the issue:
I tried
$(document).ready(function(){
alert(jQuery.inArray("Aborremåla", allaOrter));
});
but it results in "-1" (Not found).
I'm trying to find out the index nr of an item in the array. Any ideas?
Log result:
mysql_*
functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. – orourkek Aug 10 '12 at 16:39allaOrter[0]
is undefined - why is that so? Do a console.log(allaOrter) in your JavaScript and check the developer console in Chrome/Firefox etc. What does it say? – Anders Holmström Aug 10 '12 at 16:44