0

in my script php i need to format an array end encode it an a json format starting from a select like this:

    $result = mysql_query("SELECT datet,vototags,votodom FROM intro_s1 WHERE intro_s1.www = '".$url."' ORDER BY intro_s1.datet DESC");

    $data = array( 
         array('Date', 'v1', 'v2'), 

    );

     while ($row = mysql_fetch_array($result)) {
         $data[] .= array($row['datet'], $row['vototags'], $row['votodom']);
     }


echo json_encode($data);

I need that $ date is as follows:

[[Date,vi,v2],[11/02/2011,12,32],[12/06/2012,99,109][...]

instead my result is:

[["Date","v1","v2"],"Array","Array","Array","Array",[...]

ehat is wrong in my code?

Thanks in advance

3
  • Change $data[] .= to $data[] =. Commented Nov 26, 2013 at 13:27
  • Please be aware that the mysql functions have been deprecated for quite some time now. You should really start moving to mysqli or PDO. Commented Nov 26, 2013 at 13:29
  • array_push would be great in this situation: php.net/manual/en/function.array-push.php Commented Nov 26, 2013 at 13:32

1 Answer 1

2

You're using string concatenation with .= on this line:

$data[] .= array($row['datet'], $row['vototags'], $row['votodom']);

Doing so converts the array into a string which gives you the Array value.

If you remove the ., it should give you the results you're aiming for:

$data[] = array($row['datet'], $row['vototags'], $row['votodom']);

Alternatively, if you really want to make sure you're adding a new element into the array, you can use array_push() (if you don't care about the overhead of a function-call, that is):

array_push($data, array($row['datet'], $row['vototags'], $row['votodom']));
Sign up to request clarification or add additional context in comments.

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.