Hi,
I have a php array called $units which I have tried to turn into a JS object or array of objects called newUnits
But I know there is something wrong because I can’t get newUnits.length to output.
<?php
$units = ['1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8];
?>
<script>
var newUnits = <?php echo json_encode($units, JSON_PRETTY_PRINT) ?>;
var newTopics = <?php echo json_encode($topics, JSON_PRETTY_PRINT) ?>;//converts php arrays to JS object
var newSubTopics = <?php echo json_encode($subtopics, JSON_PRETTY_PRINT) >;?
function populateOne(s1, s2){
console.log(newUnits.length);
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
var optionArray = [];
s2.innerHTML = "";
............................
}
The page source shows the newUnits is not the same format as newTopics.
<!DOCTYPE html>
<html lang="en">
<head>
<script>
var newUnits = {
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8
};
var newTopics = [
{
"id": "47",
"topic": "Mechanics",
"unit": "1"
},
{
"id": "51",
"topic": "Materials",
"unit": "0"
},
{
"id": "52",
"topic": "heat",
"unit": "3"
},
{
"id": "53",
"topic": "Friction",
"unit": "4"
},
Thanks,
Shane