3

I am just beginning to learn PHP, javascript and mySQL.

I have managed to use PHP to turn mySQL results into json array:

var arrayObjects = <?php echo json_encode($results); ?>

Now I have a javascript array, which I don't know how to use.

If I view "arrayObjects" in the browser console, it shows the following:

[Object]
   0: Object
     ID: "1"
     Theme: "2"
     Item1: "0"
     Item2: "1"
     Item3: "2"

I would like to use "Theme" as an int in javascript, but I have no idea how I can get that as an int variable.

3
  • 1
    try parseInt(arrayObjects[0].Theme) or +arrayObjects[0].Theme as a shortcut. Commented Jul 3, 2015 at 11:09
  • Do you mean by the sentence "I have a javascript array, which I don't know how to use" that you can't iterate through $results in javascript or just "how I can get that as an int variable" ? Commented Jul 3, 2015 at 11:32
  • Phuzi's comment helped me out. The problem, was I didn't know the syntax on how to parse the arrayObjects "Theme" into int. Commented Jul 3, 2015 at 13:36

2 Answers 2

1

You can move the value out of the array in to its own variable, here is a working example:

<?php $results = array( "ID" => 1, "Theme" => 2); ?>
<script>

var arrayObjects = <?php echo json_encode($results); ?>

var Theme = arrayObjects['Theme'];

document.write(Theme);

</script>

I think you would need to do:

var Theme = arrayObjects[0].Theme;
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should use arrayObjects[0].Theme

1 Comment

Seems to be comment which is already posted over here

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.