Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php json get value by key

// json object.
    $contents = '{"firstName":"John", "lastName":"Doe"}';

    // Option 1: through the use of an array.
    $jsonArray = json_decode($contents,true);

    $key = "firstName";

    $firstName = $jsonArray[$key];

// Option 2: through the use of an object.
    $jsonObject = json_decode($contents);

    $key = "firstName";

    $firstName = $jsonArray->$key;
 
PREVIOUS NEXT
Tagged: #php #json #key
ADD COMMENT
Topic
Name
6+1 =