I am currently building a table that stores data it receives from an api endpoint via url. I want to add additional columns from a different endpoint. The Data is stored in JSON format and the two datasets have one key in common that is the item name.
I cant think of a way to merge these two datasets by their related key. I just started coding like a month ago. Do I need to build a database that stores the data or can i do it with php? I expirimented with mySQL but i couldent find a way to load the data from the url into my database.
Thank you very much for your help!
So i pretent to have the following data in base.json
[{ "id": 4, "name": "james" }, { "id": 6, "name": "tim" }]
and in 2nd.json
{ "4": ["flower", "car"], "6": ["house", "server"] }
so now the following PHP-Code will merge them
<?php
$api_endpoint = json_decode(file_get_contents('base.json'), true);
$additional = json_decode(file_get_contents('2nd.json'), true);
$merged_data = array();
foreach ($api_endpoint as $data) {
$merged_data[] = array_merge($data, $additional[$data['id']]);
}
var_dump($merged_data);
The Result would look like this:
["0"] => Array(
["id"] => int(6)
["name"] => str("james")
["0"] => str("flower")
["1"] => str("car")
)
["1"] => Array(
["id"] => int(6)
["name"] => str("tim")
["0"] => str("house")
["1"] => str("server")
)
So now you could use the $merged_data
and insert it into your database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.