skip to Main Content

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


  1. Chosen as BEST ANSWER

    Finally i have make a code :D many foreach in foreach

     $results = json_decode($content, true);
    
    
         echo '<br><table>';
    
               foreach($results as $result): ?>
            <tr>
    
    <td><?PHP echo $result['countryCode']; ?></td>
    
    
    <td><?PHP  foreach($result['mailTypes'] as $mailtype): ?>
            <tr>
    
    <td><?PHP echo $mailtype['mailType']; ?></td>
    
    <td><?PHP foreach($mailtype['services']  as $service): ?>
            <tr>
    
    
    <td><?PHP echo $service['serviceType']; ?></td>
    
    <td><?PHP  foreach($service['weights']  as $name): ?>
            <tr>
               <td><?PHP echo $name['name']; ?></td>
                 <td><?PHP echo $name['weightFromGram']; ?>-<?PHP echo $name['weightToGram']; ?></td>
                   <td><?PHP echo $name['sendingTariff']['amount']; ?></td>
    
            </tr>
        <?php endforeach; ?></td>
    
    
            </tr>
        <?php endforeach; ?></td>
    
            </tr>
        <?php endforeach; ?></td>
    
            </tr>
        <?php endforeach;
    
                     echo '</table>';
    

    OUTPUT:

    GB  
    MediumCorrespondence    
    Tracked 
    1-100 g.    1-100   4.15
    101-250 g.  101-250 5
    251-500 g.  251-500 5.25
    501-1000 g. 501-1000    7.65
    1001-1500 g.    1001-1500   10.75
    1501-2000 g.    1501-2000   13.25
    Ordinary    
    1-100 g.    1-100   2.25
    101-500 g.  101-500 3.8
    501-1000 g. 501-1000    5.75
    1001-2000 g.    1001-2000   9.7
    Registered  
    1-100 g.    1-100   5.6
    101-500 g.  101-500 7.15
    501-1000 g. 501-1000    9.2
    1001-2000 g.    1001-2000   13.2
    

    Also everything will be inserted in wp database with cron regular price update:

     $allPosts = $wpdb->get_results("SELECT * FROM wp_pastas WHERE countryCode LIKE '".$result['countryCode']."' AND amount='".$name['sendingTariff']['amount']."'");
    
    
    if (count($allPosts) == 0) {
    
          $updated =  $wpdb->update($tablename, array('amount'=>$name['sendingTariff']['amount']), array('weightFromGram'=>$name['weightFromGram']));
    
                   if ( $updated == 0 ) {
        $wpdb->insert( $tablename, array(
                'countryCode' => $result['countryCode'],
                'weightFromGram' => $name['weightFromGram'],
                'weightToGram' => $name['weightToGram'],
                'amount' => $name['sendingTariff']['amount'] ),
                array( '%s', '%s', '%s', '%s' )
            );
    }
    

  2. 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?

    <?php
    $arrayString = "
    array(
        array(
            'countryCode' => 'GB',
            'mailTypes' => array(
                array(
                    'mailType' => 'MediumCorrespondence',
                    'services' => array(
                        array(
                            'serviceType' => 'Tracked',
                            'weights' => array(
                                array(
                                    'name' => '1-100 g.',
                                    'weightFromGram' => 1,
                                    'weightToGram' => 100,
                                    'sendingTariff' => array(
                                        'tariffType' => NULL,
                                        'amount' => 4.15,
                                        'variantId' => 1144254,
                                        'includedTariffTypes' => array('Priority'),
                                        'requiredTariffTypes' => NULL
                                    ),
                                    'availableTariffs' => array()
                                )
                            )
                        )
                    )
                )
            )
        )
    )";
    
    // Your string evaluated to code
    $array = eval("return $arrayString;");
    
    // Check diffrences between diffrent output approach
    
    var_dump($array); // Most similar to what u gave us
    
    print_r($array); // Common output variable to text
    
    echo json_encode($array)); // Output what u gave us but as valid json
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search