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

1
  • is that JSON? json_decode Commented Sep 24, 2014 at 15:54

1 Answer 1

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"
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, totally forgot to check what can I do with json. this solved it for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.