0

I am trying to create multidimensional array from MySQL query

Query:

    $STH = $DBH->query( "SELECT  value, o_ID, oName, date,  
                         DATE_FORMAT(date, '%d %m %Y') as FDate,
                         DATE_FORMAT(tsTime, '%H:%i') as FTime
                         FROM test tst
                         LEFT JOIN object o
                         ON  tst.o_ID =o.oID
                         WHERE DATE(date) = '$date'
                         ORDER BY FDate, FTime, oName ASC");
    $STH->setFetchMode(PDO::FETCH_ASSOC); 

Loop:

$returnValue = array();
$data = array();

while ( $row = $STH->fetch() ) {

   $returnValue[$row['oName']] = 
       array(
         $data[] = array(
              'time' => $row['FTime'], 'value' => $row['value']
          )
       );
}  

Output:

{"objectA":[{"time":"23:55","value":"15"}],"objectB":[{"time":"23:55","value":"15.90"}],..}

how can I put all values in $data array?

Desired output:

{"objectA":[{"time":"01:00","value":"15"},{"time":"02:00","value":"11"},{"time":"03:00,"value":"16"}],"objectB":[{"time":""01:00","value":"12"},{"time":""02:00","value":"25"},{"time":""03:00","value":"5"}],..}
2
  • try replacing while loop as while ( $row = $STH->fetch() ) { $returnValue[$row['oName']] = array( 'time' => $row['FTime'], 'value' => $row['value'] ); } Commented Jun 28, 2013 at 8:53
  • it will return it like this: [{"time":"01:00","value":"15"},{"time":"02:00","value":"11"},....] Commented Jun 28, 2013 at 8:59

2 Answers 2

1

Give this a shot:

$returnValue = array();
while ( $row = $STH->fetch() )
{
   if (!$returnValue[$row['oName']])
   {
       $returnValue[$row['oName']] = array();
   }
   $returnValue[$row['oName']][]= array(
          'time' => $row['FTime'], 'value' => $row['value']
      );
}

That should give you what you want.

The difference here is that, if the oName-key doesn't exits, I create it, and assign it a new, empty array, then I add a new assiciative array to that array.
When the oName of row N already has a matching key, I'm not going to reassign that key, but I'm just going to push an extra array to that key.

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

2 Comments

for the first few rows it says Notice: Undefined index:objectA in...,Notice: Undefined index:objectB in... but then is ok ` {"objectA":[{"time":"00:00","value":"15.90"},{"time":"01:0","value":"15.80"}...`
@inTry: just initiate $returnValue and assign it an array, if the notices still appear, try using array_key_exists or empty instead of !$returnValue[] in the if statement
1

Try this:

while ( $row = $STH->fetch() ) {
    $returnValue[] = array(
       'time' => $row['FTime'], 'value' => $row['value']
      );
    }  

1 Comment

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in...

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.