0

How can I use array_push to add a timestamp and a value to a two-dimensional array with square bracket syntax?

I successfully get rows, each with a timestamp and associated value from a mysql database. While retrieving the results, I add these times and values into an array like so:

while($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){   
$q1_array[$i][0]= $row["time"];             
$q1_array[$i][1]= $row["val"]; // the value ("val") will either be 1 or 0 (ON or OFF)
$i++;
}

I need the final array to contain an even number of elements (which will be within a time interval of 30 minutes), so I test for that:

If the LAST array element(s?) has a timestamp and associated value of 1, I want to append at the end of the array the ending half-hour timestamp along with a 0.

if ($q1_array[sizeof($q1_array)-1][1] == 1){ 
//here I want to append a timestamp and value                       
}

On the other hand, if the FIRST element(s?) has a timestamp with associated value of 1, I want to append at the beginning of the array the starting half-hour timestamp along with a 0.

else if ($q1_array[0][1]== 1){ 
//here I want to append a timestamp and value
}

Really would appreciate help! Thank you!

Don
  • 863
  • 1
  • 8
  • 22
user983522
  • 1
  • 1
  • 3

2 Answers2

0

To add a new row to the end of the array, write:

$new_row = array($timestamp, 0);
$q1_array[] = array($timestamp, 0);

To insert at the beginning of the array, use array_splice:

array_splice($q1_array, 0, 0, $new_row);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks very much, I'll see if that works and then mark it as solved. And yes, Makita, in the case of a last entry in the array of for example (12:15; 1), the newly appended timestamp should be 12:30 and a 0. – user983522 Aug 11 '12 at 11:33
  • Thanks very much - both suggestions 'work'. However, for some reason when I try and test for the values of respectively the first element: $q1_array[0][1] or the last element: $q1_array[sizeof($q1_array)-1][1] I get an undefined index error. I suspect the error is because of the row index (i.e. either 0 or sizeof($q1_array)) but why??? I don't know whether it's the way I've defined $q1_array in the first place with square bracket notation and numeric indices to represent rows and columns? Should I be using associative keys?? Or what could be the problem? Thanks!! – user983522 Aug 12 '12 at 15:30
  • 1
    The errors you're getting would happen if your query returns no rows, so there is no $q1_array[0]; you need to check that `count($q1_array)>0`. I would certainly use an associative array for the second dimension, there's nothing ordered about time and val. Then your loop for building the array could just do `$q1_array[] = $row;`. – Barmar Aug 13 '12 at 15:11
0

For your specific question:

//process first element
if($q1_array[0][1] == 1){
    $time = roundTime($q1_array[0][0], 'start');
    $newElement = array( $time, 0 );
    array_unshift($q1_array, $newElement);
}

//process last element
$len = count($q1_array) - 1;
if($q1_array[$len][1] == 1){
    $time = roundTime($q1_array[$len][0], 'end');
    $newElement = array( $time, 0 );
    array_push($q1_array, $newElement);
}

//rounding function
//$timeIn = unix timestamp, $direction = 'start' or 'end'
function roundTime($timeIn , $direction){
    $tUnit = 1800; //seconds in half-hour
    if ($direction == 'start'){
        $output = floor($timeIn / $tUnit) * $tUnit;
    } else {
        $output = ceil($timeIn / $tUnit) * $tUnit;
    }
    return $output;
}

This works with unix timestamp format. If using MySQL datetime you'll need to convert accordingly.

Makita
  • 1,812
  • 12
  • 15
  • Please see my latest comment just above this block of code. Thanks! – user983522 Aug 12 '12 at 15:31
  • I checked again: the error in the browser is "Undefined offset: 0 " – user983522 Aug 12 '12 at 15:34
  • That typically means that the variable/element is not set. Do a print_r of the array to make sure it has at least one element, and that the structure of the array is compatible with the example script. – Makita Aug 12 '12 at 16:23