For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > June 2005 > Re: [PHP-DB] Creating an Associative Array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Creating an Associative Array
Brent Baisley

2005-06-09, 3:56 pm

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@mdanderson.org 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com