471,336 Members | 877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Multi-dimensional arrays, wildcards

Hi guys,

I've got an array question. I've searched, but maybe I just can't
figure out the best way to phrase the question to Google or groups, so
I'll try here.

I've got a multi-dimensional array that resolves out to look like this:

Array
(
[2004-48] => Array
(
[linesadded] => 122
[linesdeleted] => 4
)

[2004-49] => Array
(
[linesadded] => 28
[linesdeleted] => 23
)

[2004-50] => Array
(
[linesadded] => 18
[linesdeleted] => 5
)

[2004-51] => Array
(
[linesadded] => 178
[linesdeleted] => 38
)
)

(but quite a bit longer)

The first level key is the year and week of year. Eg, 2004-51 is the
51st week of 2004. The two second level items are what I want to give
to an array function. I'm doing some graphs, and I used to just hand
an array of values off (eg, $array=(23,25,14,23,53) and so on) and then
pass a second array that was the list of weeks in order that attach to
each data point.

I want to re-write my function to store the data keyed off date in the
array so I can do a bunch of other stuff with it without seperating the
keys from the data itself.

The challenge that I have is to, without faking it and generating
secondary arrays to use as temps, pass variations of this main array to
two different functions. One time, I want it to give a list of the
keys (to use as the tick interval on the chart) which is the first
level key, and the other function, I want to be able to disgorge, in
the order of the array, the values of all 'linesadded' or
'linesdeleted' as an array.

I assumed (whoops) that it would be as easy as, for the latter, do
something like this:

$linesadded = $VelocityData[]['linesadded']
$linesdeleted = $VelocityData[]['linesdeleted']
and for the axis
$timeaxis = array_keys($VelocityData);

It seems to be a bit more complicated. Any suggestions? If there's a
rudimentary bit of array logic that I'm missing, lemme know. If I'm
making things way too hard for myself, let me know that I'll work
around it, but it'd sure be nice to keep the keys and data together.

Thanks!

Jul 17 '05 #1
1 2119
On 23 May 2005 14:32:32 -0700, Ben Hallert wrote:
The two second level items are what I want to give
to an array function.


You need to transpose the array:

foreach ( $a as $k1 => $v1 )
foreach ( $v1 as $k2 => $v2 )
$b[$k2][$k1] = $v2;

Then use $b['linesadded'].
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
5 posts views Thread by bobwansink | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.