1

I have an example array get form database column like this :

[0] => {"data_1":"content_1","data_2":"content_2"}
[1] => {"data_1":"content_1","data_2":"content_2"}

How do i decode this json and loop it in foreach php ? Thank you in advance.

4 Answers4

2

Try below code

$array=array('{"data_1":"content_1","data_2":"content_2"}','{"data_1":"content_1","data_2":"content_2"}');
foreach($array as $a)
{
   $data=json_decode( $a );
    //print_r($data);
    foreach($data as $k=>$d)
    {
        echo $k.':'.$d;
        echo "<br>";
    }
}

Output

data_1:content_1
data_2:content_2
data_1:content_1
data_2:content_2
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
0

You may to have to start like this:

$JSONGetter = json_decode($GETjson, true);
foreach ($JSONGetter as $value){
$DataContent = $value['data_1'];
Kaan2106
  • 100
  • 11
0
var_dump(array_map(function ($value) {
    return json_decode($value, true);
}, [
    0 => '{"data_1":"content_1","data_2":"content_2"}',
    1 => '{"data_1":"content_1","data_2":"content_2"}'
]));
bailian
  • 16
  • 1
  • 2
0

try this below code

<?php 
     $nomor = 1; 
     foreach ($data_array as $data) : 
        $data_student_array = json_decode($data->DATA_STUDENT, true); ?> 
        <?php
         $nomor1 = 1; 
        foreach($data_student_array as $data_student_value)
        {
            ?>
             <tr> 
                <td><?php echo $nomor1; ?></td> <td><?php echo $data_student_value['data_1']; ?></td> 
                <td><?php echo $data_student_value['data_2']; ?></td> 
           </tr> 
            <?php
            $nomor1++; 
        }
        ?>

       <?php $nomor++; 
    endforeach; 
    ?>
Shital Marakana
  • 2,817
  • 1
  • 9
  • 12