I have json array and i want output with php in table and then put everything in database.
json:
JSON
array (
0 =>
array (
‘countryCode’ => ‘GB’,
‘mailTypes’ =>
array (
0 =>
array (
‘mailType’ => ‘MediumCorrespondence’,
‘services’ =>
array (
0 =>
array (
‘serviceType’ => ‘Tracked’,
‘weights’ =>
array (
0 =>
array (
‘name’ => ‘1-100 g.’,
‘weightFromGram’ => 1,
‘weightToGram’ => 100,
‘sendingTariff’ =>
array (
‘tariffType’ => NULL,
‘amount’ => 4.15,
‘variantId’ => 1144254,
‘includedTariffTypes’ =>
array (
0 => ‘Priority’,
),
‘requiredTariffTypes’ => NULL,
),
‘availableTariffs’ =>
array (
),
),
my code:
$results = json_decode($content, true);
echo '<table>';
foreach($results as $result){
echo '<tr>';
echo '<td>'.$result['countryCode'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<table>';
foreach($result['mailTypes'] as $mailtype){
echo '<tr>';
echo '<td>'.$mailtype['mailType'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<table>';
foreach($results['services'] as $service){
echo '<tr>';
echo '<td>'.$service['serviceType'].'</td>';
echo '</tr>';
}
echo '</table>';
Output:
GB
MediumCorrespondence
Warning: Invalid argument supplied for foreach() in
2
Answers
Finally i have make a code :D many foreach in foreach
OUTPUT:
Also everything will be inserted in wp database with cron regular price update:
At first text u provide it’s not Json but more like output of var_dump, so at this point u can’t use json_decode at this string
Also the thing u call json ends with "," so it cannot be valid anything
If you need to parse it you can try eval but mind it can make your code vulnerable (atleast as i know) there’s some old posts about it, im not sure how if it’s still same in php 7/8
When is eval evil in php?