Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8379685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:15:24+00:00 2026-06-09T16:15:24+00:00

This code, which I’ve found somewhere online, works fine and it’s kind of what

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:15:25+00:00Added an answer on June 9, 2026 at 4:15 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which works. $(document).ready(function(){ $('form').live('submit', function(){ // have to do it
I have this code which works fine for adding up input boxes. $.fn.sumValues =
I have this code which works well: Public Function LoadBinaryFile(strFilename As String) As Byte()
I have this code which is called at an onChange event of an function
I have this code which compiles and works as expected: class Right {}; class
I have this code which export data from GridView to csv. It works with
I used this code which found on forum without errors in 4.0.2a and now
I found this code which is quite nice: + (UIImage *) imageWithView:(UIView *)view {
I want to modify this code which works pretty good but (or I don't
I have this code which I am trying to change the ID of on.hover.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.