0
$sql = "SELECT * FROM `scripts` LIMIT 0, 30 ";   
$result = mysql_query($sql) or die ('Error updating database: ' . mysql_error()); 
$json = array(); 
if(mysql_num_rows($result)) { 
    while($row=mysql_fetch_row($result)) { 
        $json['table data'][]=$row;
    } 
 } 
$encoded = json_encode($json); 
echo $encoded; 

This is my output:

{"table data":[["1","test","30","13"],["2","test2","40","14"]]}

How do I access an individual piece of the array, its an array of arrays? do i decode it first?

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Jason Portnoy
  • 757
  • 8
  • 23
  • 3
    where do you wanna use it? – itachi Apr 11 '13 at 21:46
  • Why are you encoding it in the first place? – showdev Apr 11 '13 at 21:47
  • Welcome to Stack Overflow! On a side note; [please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 11 '13 at 21:48

3 Answers3

1

Try to decode

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

Bye Bye

Pierre Lebon
  • 445
  • 1
  • 5
  • 12
0

you use

 $json['table data'][$id]

where $id is the row you want to access

so for example

$json['table data'][0][1]  == "test"
$json['table data'][1][1]  == "test2"
exussum
  • 18,275
  • 8
  • 32
  • 65
0

Create an array, when you add data into array.

$json['table data'][] = array($row);
bvarga
  • 716
  • 6
  • 12