0

I have a string like this:

$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';

How can I access items in it?


I can use regex to select them, something like /"file_id":"(.*?)"/. But that's not clean at all. Is there any approach to make a array (or an object) of string above?

2 Answers 2

3

It's a json string.
You need to decode it with json_decode.
The second argument (true) is to make it an array.

$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';


$arr = json_decode($str, true);
Var_dump($arr);

https://3v4l.org/9BFIC

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you pal ...!
@Martin no problem.
-1

Explode(“,{”, $str); will work for the above.

You will get array value for each file.

1 Comment

This is a very bad solution. It is a json string and should be handled as such. Exploding like you suggest results in an array containing unusable strings

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.