Jump to content

[solved] Array composition


448191

Recommended Posts

Hmmm... It's still a bit too bulky I feel:

[code]<?php
function is_odd($var)
{
  return($var & 1);
}
$arr = array('key1','value1','key2','value2');
$keys = array_flip(array_filter(array_flip($arr), 'is_odd'));
$values = array_diff($arr,$keys);
$arr = array_combine($keys,$values);
?>[/code]

Thanks!  :)

End result:
[code]<?php
function array_compose($arr){
$arr2 = array_chunk($arr,2);
foreach($arr2 as $a){
$res[$a[0]] = next($a);
}
return $res;
}
$arr = array('key1','value1','key2','value2','emptyindex');
print_r(array_compose($arr));
?>[/code]

Outputs:
[quote]Array
(
    [key1] => value1
    [key2] => value2
    [emptyindex] =>
)[/quote]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.