0

I want to creat json array from mysql db,I try to use mysql query results to create json array object as below ,

$sql = "select DateTime ,Val1 from my table order by DateTime ASC ;";
$result = $db->query($sql);
$data = array();
$rowary = array();

while($row = mysqli_fetch_array($result))
 {
    $rowary['DateTime'] = $row['DateTime '] ;
    $rowary['Val1'] = $row['Val1 '] ;
    array_push($data,$rowary);  
  }
echo '<pre>' . var_export($data, true) . '</pre>';

the echo results is:

Array
 (
   [0] => Array
    (
        [DateTime] => 2017-02-09 12:27:23
        [Val1] => 21.0333
    )

   [1] => Array
    (
        [DateTime] => 2017-02-09 16:18:13
        [Val1] => 23.116699
    )

 )

but I want the results like this:

  Array
 (
   Array
    (
        [DateTime] => 2017-02-09 12:27:23
        [Val1] => 21.0333
    )

   Array
    (
        [DateTime] => 2017-02-09 16:18:13
        [Val1] => 23.116699
    )


 )

please tell me how do I do that?

1
  • 1
    It is the same thing. To access the first element you should use print_r($data[0]); . That would print the first array which holds 2017-02-09 12:27:23 as DateTime. The 0,1,2.... are the keys/indexes . You can't have arrays without keys. Commented Apr 21, 2017 at 9:30

1 Answer 1

2

with Array structure there must be way for you to access those array value, with the new one you are proposing you can't access the content, normal array is [value,value2,....,valuen] and those are indexed automatically for you as 0,1,...,n

For you, the array has inner array, which must follow the order explained above since, the parent is an array of Array=>[array1, array2,....,arrayn]

and in turn those inner array must also act like the parent array, which must take in values per slot in the data structure.

So yours won't go on well, or may be you are thinking of something else (data structure) but not array.

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.