Re: Creating an Associative Array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Not quite following your code completely. For instance, where does array(3601) come from?

Your loop is putting and array into an slot in another array. I think what you are trying to do is just put a value in a named slot, like this:

for($counter = 0; $counter <= 3600; $counter++)
{
$dateArray[ array(date("Y.m.d H:i:s", $now - $counter) ] = (int)ceil(980 - ($counter * .2333333333333)));
}

That should give you the output you are looking for.

On Jun 8, 2005, at 5:04 PM, dpgirago@xxxxxxxxxxxxxx wrote:

This question will probably be off-topic for some, but it does relate to a
database application so please bear with me.

I'm pulling date-times out of a MySQL db:

mysql> select date_format(`date_time`, '%Y.%m.%d %H:%i:%s') as rightNow
from location1 order by date_time desc limit 1;

+----------------------------+
| rightNow                     |
+----------------------------+
| 2005.06.08 14:24:11 |
+----------------------------+
1 row in set (0.00 sec)

then trying to create an associative array where the date-time is the key
and a value, corresponding to a pixel location on a graph, is computed
using an algorithm like this:

(int)ceil(980 - ($counter * .2333333333333)); (see below)

This code snippet is producing an array of arrays rather than a simple
associative array:

<?php

$now = time();
//$dateArray = array(); (output is the same with or without this line)

for($counter = 0; $counter <= 3600; $counter++)
{
      $dateArray[] = array(date("Y.m.d H:i:s", $now - $counter) =>
(int)ceil(980 - ($counter * .2333333333333)));
}

?>

output:

array(3601) { [0]=> array(1) { ["2005.06.08 15:50:17"]=> int(980) } [1]=>
array(1)
{ ["2005.06.08 15:50:16"]=> int(980) } [2]=> array(1) { ["2005.06.08
15:50:15"]=> int(980) } [3]=> array(1)
{ ["2005.06.08 15:50:14"]=> int(980) } [4]=> array(1) { ["2005.06.08
15:50:13"]=> int(980) } [5]=> array(1)
{ ["2005.06.08 15:50:12"]=> int(979) } [6]=> array(1) { ["2005.06.08
15:50:11"]=> int(979) } [7]=> array(1) ...

What I need is output like this:

array(3601) { ["2005.06.08 14:52:56"]=> int(980)
                  ["2005.06.08 14:52:55"]=> int(979)
                      ["2005.06.08 14:52:54"]=> int(978)
                      ["2005.06.08 14:52:53"]=> int(977)
                  etc....
                    }

so that I can search for a pixel value given a certain time value within
the past hour.

I hope this makes sense and that someone can see the error in code.

I'm using php 4.3.7, but I see that php 5.x has an array_combine function
that would do the trick.
Maybe will have to upgrade.

David
(obviously not ready for the Zend Exam)

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux