Ethan Allen
Ethan Allen

Reputation: 14835

How do I get all of the keys in this JSON with PHP?

$mystring = '{
    "display_name": "Silverware",
    "user": "joesmith",
    "id": 27,
    "field_name": "Age",
    "sort_order": 1,
    "required": 0,
    "view_type": "text"
}';

I'd like a PHP array with all of the keys from the code above. Whats the most efficient way?

Upvotes: 6

Views: 12914

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

get all of the keys in this JSON with PHP

Short solution using json_decode and array_keys functions:

$keys = array_keys(json_decode($mystring, true));

Upvotes: 15

Related Questions