0

I have List of IDs in JSON

Here it is: https://temp.9animetv.live/api.php

it look like this

 { "result": [
    548915,
    505031,
    28967,
    520928,
    441762,
    381418,
    61650,
    249457,
    535995,
    550023,
      and more.. and more.. and more..
   ]
}

I want to change it to be like this

{ 
    "result": [
        {"id": 548915,}, 
        {"id": 505031,}, 
        {"id": 28967,},
        {"id": 28967,}, 
        {"id": 28967,}
      ] 
}

How to do that? Am using PHP

I have Tried

To change it to Array using Json_decode But still haven't figured out a way to do as I expected

3
  • Why? What will that really achieve Commented Mar 9, 2023 at 13:35
  • @RiggsFolly Am using it on third part project, and that was their Example.. I was tried to use it without formatting that way... And it ddn't work.. So am trying to format it as their example Commented Mar 9, 2023 at 14:00
  • 1
    Related: PHP wrap array elements in separate arrays Commented Mar 9, 2023 at 14:57

1 Answer 1

1

Simply read the json string, convert it to a PHP equivalent object using json_decode() process the array of id's and create a new array which you then convert back to a JSON String

$j_str = file_get_contents('https://temp.9animetv.live/api.php');
$j_arr = json_decode($j_str);
$new = [];
foreach ( $j_arr->result as $occ ) {
    $new[] = ['id' => $occ];
}
echo json_encode($new, JSON_PRETTY_PRINT);

RESULTS

[
    {
        "id": 548915
    },
    {
        "id": 505031
    },
    {
        "id": 28967
    },

    . . .

You can skip the , JSON_PRETTY_PRINT, thats only I could easily read the output for checking

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.