-3

how to extract from json

{"aaData":[["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:37","5"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:38","6"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:40","9"],["fdsf","fds","654545","2016-02-08 13:52:40","12"],["fsdf","fsdfsdfds","546","2016-02-08 13:53:51","13"],["hjgh","hg","3123123","2016-02-08 14:35:31","14"]]}


foreach($data->aaData as $row) {
echo $row->aaData[1];   
}

Its not working please Help me someone

5 Answers5

1
$json = '{"aaData":[["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:37","5"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:38","6"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:40","9"],["fdsf","fds","654545","2016-02-08 13:52:40","12"],["fsdf","fsdfsdfds","546","2016-02-08 13:53:51","13"],["hjgh","hg","3123123","2016-02-08 14:35:31","14"]]}';

$data = json_decode ($json, TRUE);

foreach($data['aaData'] as $row) {
    var_dump ($row);   
}

I would use json_decode (here documentation) and then will manage to post the data.

SpongePablo
  • 870
  • 10
  • 24
1

JSON is not an array, an object, or a data structure. JSON is a text-based serialization format - so a fancy string, but still just a string. Decode it in PHP by using json_decode().

$data = json_decode($json);

For Example:-

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));

will output

object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }

array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }

More over in your case it will be like

$data='{"aaData":[["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:37","5"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:38","6"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:40","9"],["fdsf","fds","654545","2016-02-08 13:52:40","12"],["fsdf","fsdfsdfds","546","2016-02-08 13:53:51","13"],["hjgh","hg","3123123","2016-02-08 14:35:31","14"]]}';
$yourResult=json_decode($data);
echo "<pre>";
print_r($yourResult);
foreach ($yourResult->aaData as $item) {
//do something with this data
}

Hope it helps!

Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
1

php has in-built function json_decode() below is the example

<?php
$rs=json_decode($jsonvalue);
print_r($rs);
?>

see php official documentation

** your question o/p** enter image description here

This is my updated code

$string="";
$data=json_decode('{"aaData":[["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:37","5"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:38","6"],["Hitech Institute","0shoaib0@gmail.com","8149587579","2016-02-04 16:55:40","9"],["fdsf","fds","654545","2016-02-08 13:52:40","12"],["fsdf","fsdfsdfds","546","2016-02-08 13:53:51","13"],["hjgh","hg","3123123","2016-02-08 14:35:31","14"]]}');
$string.="<table border=1>";
foreach ($data->aaData as $key => $value) {
    $string.="<tr>";
    $string.="<td>";
    $string.="<a href=".'yourpath/'.$value[4].">View</a>";
    $string.="<a href=".'yourpath/'.$value[4].">Edit</a>";
    $string.="<a href=".'yourpath/'.$value[4].">Delete</a>";
    $string.="</td>";
    $string.="</tr>";
}
$string.="</table>";
echo $string;

this is o/p you add your bootstrap class and font icons your table o/p

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
0

PHP foreach loop does't directly work on json data. You need to decode in array first as following

$data = json_decode($JSON);

Check documentation

0

In order to get an array with the information of the JSON you need to do this:

$output_array=json_decode($json);

After that you can go trough the information inside with

foreach ($output_array->aaData as $item) {
    //....do something
}
DreamWave
  • 1,934
  • 3
  • 28
  • 59