Q:

How to get the position integer of an item in php array from php to html?

I'm trying to return the position of a topic as well as the topic in a getTopics() function. For example, returning "Musical instruments" would also return the integer 0 which can then be used in the getFaqs() function. Returning "Programming languages" would also return the integer 1 and so on. I don't understand why this function isn't working as I've included parameters topic_id (the integer) and topic (the text).
index.html:
function getTopics() {
    $.getJSON("topics.php", function(result) {
        $("#topic-container").html("");
        $.each(result, function(topic_id, topic){
            $("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos 
        });
    });
    return false; 
}

topics.php:
<?php

header('Content-Type: application/json; charset=utf-8');
$topics = array("Musical instruments", "Programming languages", "Varieties of pizza");
?>

A:

Just use a standard for loop on the array:
var topic, topic_id;
for (topic_id = 0; topic_id < result.length; topic_id++) {
    topic = result[topic_id];
    $("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos
}

