0

In my database I have POSTed following JSON with REST API:

{
    "author": "Someone",
    "hero_name": "Iron Man",
    "hero_desc": ["Iron", "Man"],
}

Now in database Table, the entry says only Array for the hero_desc.

I cannot figure out how to get the actual items in that array.. for example to fetch Iron from hero_desc.

Here is how it gets returned with GET request:

{
    "id": "11",
    "user_id": "1",
    "author": "Someone",
    "hero_name": "Iron Man",
    "hero_desc": "Array",
}

I am quite new to PHP so I am worried that I would need to mess up my whole app architecture to get this to work. I though PHP can handle fetching arrays by default if it sees it's an JSON Array.

Dharman
  • 30,962
  • 25
  • 85
  • 135
AndroidDev101
  • 122
  • 1
  • 13
  • check this document of mySQL https://dev.mysql.com/doc/refman/8.0/en/json.html – prasanna puttaswamy Sep 30 '18 at 11:25
  • 1
    Possible duplicate of [json\_decode to array](https://stackoverflow.com/questions/5164404/json-decode-to-array) – Andreas Sep 30 '18 at 11:26
  • Are you trying to save it as JSON in your database (with the json-datatype) or are you trying to save the different values in different columns? Please show us your database schema, your code and the expected result. The question is currently very unclear. – M. Eriksson Sep 30 '18 at 11:26
  • You might also want to look into [Database Normalization](https://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/) – M. Eriksson Sep 30 '18 at 11:28

2 Answers2

0

use json_decode(); function for change json to array

$json_to_array = json_decode($your_json_variable_name);
0

You could change array data to JSON with json_encode In one hand you have an array without the key that code makes a number key for that in another hand if have an array with the key you can get that inside JSON with that key.

$content ='your content or your array data';
header('Content-Type: application/json; charset=utf8');
echo json_encode($content);