I have an object or array like below: "[{},{},{},{},{},{},{},{},{\"subject\":\"Math\"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{\"subject\":\"Chemistry\"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]"
as well: [{},{},{},{},{},{},{},{},{"subject":"Math"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{"subject":"Chemistry"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
when i am iterating it using foreach loop it show " Invalid argument supplied for foreach() in C:\wamp64\www\eschool\cms\admin\scripts\time-table-add-submit.php on line 11 " May be due to the empty value at the beginning and at the end. but I want to iterate it somehow how to do that. Heres my code: JavaScript:
var timeTableSub = [
{"subject": timeTableSub1},
{"subject": timeTableSub2},
... Like Wise....
{"subject": timeTableSub42}
];
var jsonArray = JSON.stringify(timeTableSub);
$.ajax({
url: "scripts/time-table-add-submit.php",
type: "POST",
dataType: 'html',
data:{
'arrayList': jsonArray
},
success: function (data) {
data = $.trim(data);
$("#tempDiv").html(data);
}
});
PHP:
<?php
$arrayList = json_encode($_POST['arrayList'], TRUE);
echo $arrayList."<br>";
$newArray = json_decode($arrayList);
echo $newArray."<br>";
foreach ($newArray as $key => $value)
{
echo $key . " => " . $value . "<br>";
}
?>
$arrayList = json_encode($_POST['arrayList'], TRUE);because what you've sent from the browser is already JSON. And therefore you should be writing$newArray = json_decode($_POST['arrayList']);instead later on as well