-5

I have a PHP code that dumps following array:

array(4) { 
[0]=> string(79) "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-0.m3u8 " 
[1]=> string(79) "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-1.m3u8 " 
[2]=> string(79) "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-2.m3u8 " 
[3]=> string(78) "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-3.m3u8" 
}

I want to extract each URL from above array and convert it to JSON. JSON might look like:

{"m3u8_1":"https:\/\/95.217.21.141\/hls\/2f04510c8f646cb3e03d5c063b190bd1611847725589-0.m3u8","m3u8_2":"https:\/\/95.217.21.141\/hls\/2f04510c8f646cb3e03d5c063b190bd1611847725589-1.m3u8","m3u8_3":"https:\/\/95.217.21.141\/hls\/2f04510c8f646cb3e03d5c063b190bd1611847725589-2.m3u8","m3u8_4":"https:\/\/95.217.21.141\/hls\/2f04510c8f646cb3e03d5c063b190bd1611847725589-3.m3u8"}

I don't know how to convert it into JSON. Also the tricky thing here is, that not every time there would be 4 URLS in array. It might be 1 or 2 or even 5. How do I check that how many URL does array has and then convert it to JSON according to that.

Also I tried searching on stack overflow before putting it here, but I didn't found one that checks and then convert it to JSON

Thanks!

Edit #1

Direct json_encode won't work because PHP does not return values from json which has characters such as 0 1 2 3

9
  • 2
    What's wrong with directly encoding the entire array with json_encode? Commented Jan 28, 2021 at 16:35
  • It would give wrong JSON. Like with values 0 1 2 3 And PHP does not allow to extract those values from JSON after using json_decode Commented Jan 28, 2021 at 16:37
  • 1
    Ah, I see now. So, basically, what you need is to loop this array and create a new one that has the keys you want. You can use for or foreach, whatever rocks your boat. But it's pretty basic. Commented Jan 28, 2021 at 16:40
  • 2
    Is that because of the .m3u8 at the end of the url? If so could that ever be anything else and would that change the key value you want Commented Jan 28, 2021 at 16:41
  • 1
    Well thats pretty simple, did you give anything a try Commented Jan 28, 2021 at 16:44

1 Answer 1

5

This would do the trick:

$input = [
    "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-0.m3u8",
    "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-1.m3u8",
     "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-2.m3u8",
     "https://95.217.21.141/hls/2f04510c8f646cb3e03d5c063b190bd1611847725589-3.m3u8"
];
$output = [];
$i = 1;
foreach($input as  $url) {
    $output['m3u8_' . $i] = $url;
    $i++;
}
print(json_encode($output));
Sign up to request clarification or add additional context in comments.

Comments

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.