Search code examples
phpajaxform

How can i Iterate through data returned in Ajax success?


How can I iterate to get id value? This is my array:

[{"email_id":"gayatri.dsf@detedu.org","Id":"216"}]

tried

<?php
    foreach($faculty as $value)
    {
       echo $value['Id'];
    }
?>

Gives an error

Use of undefined constant Id - assumed Id


Solution

  • This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):

    [{"email_id":"gayatri.dsf@detedu.org","Id":"216"}]
    

    You need to convert it to an array first:

    $jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
    foreach($jsonValues as $faculty) //for each user do something
    {
        echo $faculty['Id'];
    }