Multi-dimensional arrays, wildcards

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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:

    [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.


Re: Multi-dimensional arrays, wildcards

On 23 May 2005 14:32:32 -0700, Ben Hallert wrote:
Quoted text here. Click to load it

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 - /
Thunderbird E-mail and Newsgroups - /

Site Timeline