The below code has only array like ["a","b","c"] but returns [Object,Object,Object]. The result of which I need to use nested $each loop in ajax success function. Is there any better way to do it?
if($_GET['semValue'])
{
$sem_value = $_GET['semValue'];
try
{
$stmt = $dbConn->prepare("SELECT Semester FROM CourseInfo");
$semArray = array();
if ($stmt->execute())
{
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$semArray[] = $row;
}
}
echo json_encode($semArray);
exit();
}
catch(PDOException $e)
{
echo 'Exception -> ';
var_dump($e->getMessage());
}
You're inserting entire row
in main array so it will return multidimensional array instead of array of string.
To get the array of string replace the following lines.
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$semArray[] = $row["Semester"];
}
Explanation:
$row
would be like this,
$row= Array("semester"=>1);
$row= Array("semester"=>2);
// And so on
If you follow your current code then your final array would be like this,
$semArray = Array ( [0]=> Array("semester"=>1), [1]=> Array("semester"=>2));
So it becomes multidimensional array, that's why we use $row["Semester"]
to make it single dimensional array of string/integer values.