0

Hello I am not really familiar with fancy stuff that can be done with php, so i could do 200line function, when pros could do it better in few fabolus lines. Lets say i receive string that looks like

[{
    "id": "9e30bb3094e00abc291016a7a597ba1840a6d6ec",
    "user_id": "adb16da39e5ecc448e2aa4aec8a34a8158fa137a",
    "document_name": "sample.pdf",
 }]
[{
    "id": "9e30bb3094e00a6c291016a7a598ba1840a6d6ec",
    "user_id": "adb16da39e5ecc448e2aa4aec8a34a8158fa137a",
    "document_name": "sample2.pdf",
 }]

. . .

Is there any neat method to get number of these "documents", their ids. like
echo $dnumber; //5
echo $id[5]; //9e30bb3094e00a6c291016a7a598ba1840a6d6ec

Newb
  • 27
  • 1
  • 6

1 Answers1

2

Considering it's a JSON, you can use json_decode

$string = '...';
$records = json_decode($string);
echo $records[5]->id;

Valid JSON would be

[
    {
        "id": "9e30bb3094e00abc291016a7a597ba1840a6d6ec",
        "user_id": "adb16da39e5ecc448e2aa4aec8a34a8158fa137a",
        "document_name": "sample.pdf"
    },
    {
        "id": "9e30bb3094e00a6c291016a7a598ba1840a6d6ec",
        "user_id": "adb16da39e5ecc448e2aa4aec8a34a8158fa137a",
        "document_name": "sample2.pdf"
    }
]
cornelb
  • 6,046
  • 3
  • 19
  • 30