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?

3
  • 3
    where do you wanna use it? Commented Apr 11, 2013 at 21:46
  • Why are you encoding it in the first place? Commented Apr 11, 2013 at 21:47
  • Welcome to Stack Overflow! On a side note; please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use pdo or mysqli. Commented Apr 11, 2013 at 21:48

3 Answers 3

1

Try to decode

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

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

Bye Bye

Sign up to request clarification or add additional context in comments.

Comments

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"

Comments

0

Create an array, when you add data into array.

$json['table data'][] = array($row);

Comments

Your Answer

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.