-1

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

  • `$obj=json_decode($arr); echo $obj->id`? – Professor Abronsius Nov 17 '17 at 12:40
  • 1
    The pending edit https://stackoverflow.com/review/suggested-edits/17979995 should be rejected, using blockquoting. It also doesn't make much of an (positive) impact on the post. Edit: @matloobHasnain Please **do not** use blockquotes for text. You have been flagged a few times for it and may get your editing privilege revoked. – Funk Forty Niner Nov 17 '17 at 12:44
  • https://stackoverflow.com/review/suggested-edits/17979995 --- ***Community♦ reviewed this 1 min ago: Approve*** - are you guys serious??? – Funk Forty Niner Nov 17 '17 at 12:57
  • Hi, Please check the question and change the ratings. – Pooja Bajaj Dhoot Dec 12 '17 at 09:28

2 Answers2

2

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'];
}
ka_lin
  • 9,329
  • 6
  • 35
  • 56
1

This is JSON format. First you have to decode it. Example:

$a = '[{"email_id":"gayatri.dsf@detedu.org","Id":"216"}]';

$dec = json_decode($a);

echo $dec[0]->Id;

Result: 216

Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.

With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON

Blackbam
  • 17,496
  • 26
  • 97
  • 150