Please i need your a guidance,
I am working on a JSON data that i encoded into an array and it has 5 array keys and arrays or arrays in each array and i want to insert values from this arrays into the database
// The Json content below
$data = '{"Routes":[],"Quotes":[{"QuoteId":1,"MinPrice":1775.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1530],"OriginId":81727,"DestinationId":66035,"DepartureDate":"2020-08-03T00:00:00"},"QuoteDateTime":"2020-07-30T03:17:00"},{"QuoteId":2,"MinPrice":739.0,"Direct":false,"OutboundLeg":{"CarrierIds":[851],"OriginId":81727,"DestinationId":66035,"DepartureDate":"2020-08-16T00:00:00"},"QuoteDateTime":"2020-07-28T23:46:00"},{"QuoteId":3,"MinPrice":709.0,"Direct":false,"OutboundLeg":{"CarrierIds":[819],"OriginId":81727,"DestinationId":66035,"DepartureDate":"2020-08-19T00:00:00"},"QuoteDateTime":"2020-07-29T08:20:00"},{"QuoteId":4,"MinPrice":706.0,"Direct":false,"OutboundLeg":{"CarrierIds":[819],"OriginId":81727,"DestinationId":66035,"DepartureDate":"2020-08-27T00:00:00"},"QuoteDateTime":"2020-07-30T03:17:00"}],"Places":[{"PlaceId":66035,"IataCode":"LOS","Name":"Lagos","Type":"Station","SkyscannerCode":"LOS","CityName":"Lagos","CityId":"LOSA","CountryName":"Nigeria"},{"PlaceId":81727,"IataCode":"SFO","Name":"San Francisco International","Type":"Station","SkyscannerCode":"SFO","CityName":"San Francisco","CityId":"SFOA","CountryName":"United States"}],"Carriers":[{"CarrierId":819,"Name":"Aegean Airlines"},{"CarrierId":851,"Name":"Alaska Airlines"},{"CarrierId":1530,"Name":"Asiana Airlines"}],"Currencies":[{"Code":"USD","Symbol":"$","ThousandsSeparator":",","DecimalSeparator":".","SymbolOnLeft":true,"SpaceBetweenAmountAndSymbol":false,"RoundingCoefficient":0,"DecimalDigits":2}]}';
Then i did this to convert it into an array
$form = json_decode($data, true);
And then used print_r($form) to print the array and this is what i got
Array ( [Routes] => Array ( ) [Quotes] => Array ( [0] => Array ( [QuoteId] => 1 [MinPrice] => 1775 [Direct] => [OutboundLeg] => Array ( [CarrierIds] => Array ( [0] => 1530 ) [OriginId] => 81727 [DestinationId] => 66035 [DepartureDate] => 2020-08-03T00:00:00 ) [QuoteDateTime] => 2020-07-30T03:17:00 ) [1] => Array ( [QuoteId] => 2 [MinPrice] => 739 [Direct] => [OutboundLeg] => Array ( [CarrierIds] => Array ( [0] => 851 ) [OriginId] => 81727 [DestinationId] => 66035 [DepartureDate] => 2020-08-16T00:00:00 ) [QuoteDateTime] => 2020-07-28T23:46:00 ) [2] => Array ( [QuoteId] => 3 [MinPrice] => 709 [Direct] => [OutboundLeg] => Array ( [CarrierIds] => Array ( [0] => 819 ) [OriginId] => 81727 [DestinationId] => 66035 [DepartureDate] => 2020-08-19T00:00:00 ) [QuoteDateTime] => 2020-07-29T08:20:00 ) [3] => Array ( [QuoteId] => 4 [MinPrice] => 706 [Direct] => [OutboundLeg] => Array ( [CarrierIds] => Array ( [0] => 819 ) [OriginId] => 81727 [DestinationId] => 66035 [DepartureDate] => 2020-08-27T00:00:00 ) [QuoteDateTime] => 2020-07-30T03:17:00 ) ) [Places] => Array ( [0] => Array ( [PlaceId] => 66035 [IataCode] => LOS [Name] => Lagos [Type] => Station [SkyscannerCode] => LOS [CityName] => Lagos [CityId] => LOSA [CountryName] => Nigeria ) [1] => Array ( [PlaceId] => 81727 [IataCode] => SFO [Name] => San Francisco International [Type] => Station [SkyscannerCode] => SFO [CityName] => San Francisco [CityId] => SFOA [CountryName] => United States ) ) [Carriers] => Array ( [0] => Array ( [CarrierId] => 819 [Name] => Aegean Airlines ) [1] => Array ( [CarrierId] => 851 [Name] => Alaska Airlines ) [2] => Array ( [CarrierId] => 1530 [Name] => Asiana Airlines ) ) [Currencies] => Array ( [0] => Array ( [Code] => USD [Symbol] => $ [ThousandsSeparator] => , [DecimalSeparator] => . [SymbolOnLeft] => 1 [SpaceBetweenAmountAndSymbol] => [RoundingCoefficient] => 0 [DecimalDigits] => 2 ) ) )
And when i try getting the array keys using
$arraykey = array_keys($form);
print_r($arraykey);
// This is what i got
Array ( [0] => Routes [1] => Quotes [2] => Places [3] => Carriers [4] => Currencies )
Now my own approach to the problem is shown below even though i got stucked only getting the only them parts by parts which i know is not the bset approach.
foreach($form['Quotes'] as $one){
$quoteid = $one['QuoteId'];
$minprice = $one['MinPrice'];
$direct = $one['Direct'];
// then i did a sql that will insert into database (Note: am not using prepared statement here)
$sql = "INSERT INTO mytable (quoteid, minprice, direct) VALUES ('$quoteid', '$minprice', '$direct')";
mysqli_query($con, $sql);
}
// Then i did same for $form('Places')
foreach($form['Places'] as $two){
$placeid = $two['PlaceId'];
$IataCode = $two['IataCode'];
$Name = $two['Name'];
// then i did a sql that will insert into database (Note: am not using prepared statement here)
$sql = "INSERT INTO mytable (placeid, iatacode, name) VALUES ('$placeid', '$IataCode', '$Name')";
mysqli_query($con, $sql);
}
The Below Diagram is how it should look like when inserted into the database, and not adding new extra rows with empty columns making 8rows instead of normal 4 rows with its related data.
```
Here is the Sample Database Structure.
**quoteid | minprice | direct | placeid | iatacode |name**
1 | $209 | yes | 22200 | LOS | Lagos
2 | $109 | no | 12200 | NYC | New York
3 | $409 | yes | 32200 | LON | London
4 | $309 | yes | 42200 | LOS | Lagos
```
But at this Junction it dawned on me that i took the wrong step because when they got to the database they were not properly aligned.
Please I appreciate your time for reading through and will be grateful if you suggest a better way of doing this.
Thanks in Advance