[Solar-talk] another blonde array problem

Andreas Ravnestad andreas.ravnestad at gmail.com
Mon Dec 3 13:10:04 CST 2007


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? :-(
>
>   



Maybe this is what you had in mind? (one foreach was enough, it seems)

<?
$src = 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'),
);

foreach($src as $items) {
    $k = key($items);
    $v = current($items);
    $result[$k][$v] = 'ACTION_' . strtoupper(end(explode(" ", $v)));
}

?>
<pre>
<?print_r($result)?>>
</pre>


More information about the Solar-talk mailing list