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.

asked Jul 14, 2017 at 6:56
7
  • use json_decode() Commented Jul 14, 2017 at 6:58
  • json_decode( $response_string ); Commented Jul 14, 2017 at 6:58
  • I have tried but after the foreach loop that appears only one data array. Is there any solution? Commented Jul 14, 2017 at 7:00
  • please share your full code with foreach loop Commented Jul 14, 2017 at 7:01
  • 1
    Possible duplicate of How do I extract data from JSON with PHP? Commented Jul 14, 2017 at 7:15

4 Answers 4

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
answered Jul 14, 2017 at 7:02

Comments

0

You may to have to start like this:

$JSONGetter = json_decode($GETjson, true);
foreach ($JSONGetter as $value){
$DataContent = $value['data_1'];
answered Jul 14, 2017 at 7:04

Comments

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"}'
]));
answered Jul 14, 2017 at 7:06

Comments

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; 
 ?>
answered Jul 14, 2017 at 7:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.