[Solar-talk] another blonde array problem
Paul M Jones
pmjones at solarphp.com
Mon Dec 3 13:05:53 CST 2007
On 03 Dec 2007, at 13:07, Jeff Surgeson wrote:
> Hi All
>
> Got this!
> array(
> 0 => array('Power Generation' => 'Using the Sun'),
> 1 => array('Power Generation' => 'Using the Wind'),
> 2 => array('Heating & Cooling' => 'Radiant Slabs'),
> 3 => array('Heating & Cooling' => 'Domestic Hot Water'),
> );
>
> Need this!
>
> array(
> 'Power Generation' => array(
> 'Using the Sun' => 'ACTION_SUN',
> 'Using the Wind' => 'ACTION_WIND',
> ),
> 'Heating & Cooling' => array(
> 'Radiant Slabs' => 'ACTION_SLABS',
> 'Domestic Hot Water' => 'ACTION_WATER',
> ),
> );
>
> Anyone care to put me out of my misery and do this with a few foreach
> loops? :-(
You only need one loop, I think, and then you can build a $new array
based on the key/value pairs. Here's a start for you:
<?php
$old = array(
0 => array('Power Generation' => 'Using the Sun'),
1 => array('Power Generation' => 'Using the Wind'),
2 => array('Heating & Cooling' => 'Radiant Slabs'),
3 => array('Heating & Cooling' => 'Domestic Hot Water'),
);
$new = array();
foreach ($old as $item) {
$key = key($item);
$val = current($item);
$new[$key][] = $val;
}
var_dump($new);
/*
array(2) {
["Power Generation"]=>
array(2) {
[0]=>
string(13) "Using the Sun"
[1]=>
string(14) "Using the Wind"
}
["Heating & Cooling"]=>
array(2) {
[0]=>
string(13) "Radiant Slabs"
[1]=>
string(18) "Domestic Hot Water"
}
}
*/
Hope this helps. :-)
-- pmj
More information about the Solar-talk
mailing list