3

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: loggen

6
  • 4
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Aug 10, 2012 at 16:39
  • ohh, I had no idea mysql_ is moving out. Commented Aug 10, 2012 at 16:44
  • allaOrter[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? Commented Aug 10, 2012 at 16:44
  • @AndersHolmström First one is undefined because there is no "ortnamn" with index 0 so the first one is empty Commented Aug 10, 2012 at 16:47
  • @anders: because JS arrays start at 0, but sql ids generally start at 1. Commented Aug 10, 2012 at 16:47

2 Answers 2

3

The problem here is that json_encode($result); creates a JSON object, not an array. Hence jQuery.inArray isn't working.

Build a JavaScript array instead: [ "value", "value", "value", etc... ];

Or do something like this with the object:

var locations = {
    "Abårremöla" : 1,
    "Stuff" : 2,
    "Ludvika" : 1549
};

var query = "Ludvika";
if(query in locations) {
    var id = locations[query];
    alert(id); //displays 1549
}



For that to work you have to build up the object the other way around in PHP - so a PHP array with the location name as the indexer and the id as the value:

$query = mysql_query("SELECT ort_id, ortnamn FROM geo_orter");
while(($row = mysql_fetch_assoc($query))){
    $index = $row['ortnamn'];     //<- note!
    $result[$i] = $row['ort_id']; //<- note!
};
$allaOrterjson=json_encode($result);
Sign up to request clarification or add additional context in comments.

7 Comments

But can I then control the index of each item?
Each value has an id in the db so I'm trying to create a list with values and their ids as indexes so that I can look through it as the user chooses a value
Or is there perhaps a way to look through the object instead? :)
I understand the problem. What do you actually want to do with the object? Check if it has a certain value?
Updated my answer with an object-based solution.
|
0

Try this

var my_cars= new Array()
my_cars[0]="Mustang";

my_cars["family"]="Station Wagon";

my_cars["big"]="SUV";

for( var i in my_cars ){
    console.log( my_cars[i] );
}

var n = {
    "0": "Mustang",
    "family": " Station Wagon",
    "big": "SUV"
};
for( var i in n ){
    console.log( n[i] );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.