我想结合下面显示的三个数组,我不知道该怎么做,我想取每个数组的companyCode
并为一个数组创建它。
我有一个像这样的数组,
Array(
[0] => Array(
[companyCode] => SimpleXMLElement Object(
[0] => 'AD'
)
[name] => SimpleXMLElement Object(
[0] => 'NYCT01'
)
[vehicleRentalPrefType] => SimpleXMLElement Object(
[0] => 'ECAR'
)
[rateAmount] => SimpleXMLElement Object(
[0] => '295.62'
)
);
[1] => Array(
[companyCode] => SimpleXMLElement Object(
[0] => 'AD'
)
[name] => SimpleXMLElement Object(
[0] => 'NYCT01'
)
[vehicleRentalPrefType] => SimpleXMLElement Object(
[0] => 'SCAR'
)
[rateAmount] => SimpleXMLElement Object(
[0] => '356.25'
)
[2] => Array(
[companyCode] => SimpleXMLElement Object(
[0] => 'AD'
)
[name] => SimpleXMLElement Object(
[0] => 'NYCT01'
)
[vehicleRentalPrefType] => SimpleXMLElement Object(
[0] => 'PCAR'
)
[rateAmount] => SimpleXMLElement Object(
[0] => '562.36'
)
)
)
我想要这个:
Array(
[AD] => Array(
[NYCT01] => Array(
[ECAR] => Array(
[0] => '295.62'
)
[SCAR] => Array(
[0] => '356.25'
)
[PCAR] => Array(
[0] => '562.36'
)
)
)
)
我怎么能这样做,我试着做一些循环,但是我没有得到任何结果请帮助。
答案 0 :(得分:1)
这是我几天前创建的用于对数组进行分组的函数:
PHP 5.3及以上版本
class Fx
{
public static function GroupBy(&$arr, $groupby, $valueFunctions = null)
{
$groupby = is_array($groupby) ? $groupby : array($groupby);
$valueFunctions = is_array($valueFunctions) ? $valueFunctions : array($valueFunctions);
$group1 = array();
foreach($arr as $elem)
{
$group = is_callable($groupby[0]) ? $groupby[0]($elem) : $elem[$groupby[0]];
$group1[$group][] = is_callable($valueFunctions[0]) ? $valueFunctions[0]($elem) : (is_null($valueFunctions[0]) ? $elem : $elem[$valueFunctions[0]]);
}
if(count($groupby) > 1)
{
$group2 = array();
$gb_next = array_slice($groupby, 1);
$vf_next = (count($valueFunctions) > 1) ? array_slice($valueFunctions, 1) : null;
foreach($group1 as $group => $elems)
{
$group2[$group] = self::GroupBy($elems, $gb_next, $vf_next);
}
return $group2;
}
return $group1;
}
}
> = 5.3示例
$new = Fx::GroupBy(
$arr,
array(
function($elem){ return (string)$elem['companyCode'][0]; },
function($elem){ return (string)$elem['name'][0]; },
function($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
),
array(
null,
null,
function($elem){ return (float)$elem['rateAmount'][0]; }
)
);
print_r($new);
适用于PHP 5.2,可能还有
class Fx
{
public static function GroupBy(&$arr, $groupby, $valueFunctions = null)
{
$groupby = is_array($groupby) ? $groupby : array($groupby);
$valueFunctions = is_array($valueFunctions) ? $valueFunctions : array($valueFunctions);
$group1 = array();
foreach($arr as $elem)
{
$gbIsFunc = (substr($groupby[0], 0, 5) == 'func:');
$gbFunc = substr($groupby[0], 5);
$vIsFunc = (substr($valueFunctions[0], 0, 5) == 'func:');
$vFunc = substr($valueFunctions[0], 5);
$group = $gbIsFunc ? $gbFunc($elem) : $elem[$groupby[0]];
$group1[$group][] = $vIsFunc ? $vFunc($elem) : (is_null($valueFunctions[0]) ? $elem : $elem[$valueFunctions[0]]);
}
if(count($groupby) > 1)
{
$group2 = array();
$gb_next = array_slice($groupby, 1);
$vf_next = (count($valueFunctions) > 1) ? array_slice($valueFunctions, 1) : null;
foreach($group1 as $group => $elems)
{
$group2[$group] = self::GroupBy($elems, $gb_next, $vf_next);
}
return $group2;
}
return $group1;
}
}
< = 5.2示例
function gbFunc1($elem){ return (string)$elem['companyCode'][0]; }
function gbFunc2($elem){ return (string)$elem['name'][0]; }
function gbFunc3($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
function vFunc3($elem){ return (float)$elem['rateAmount'][0]; }
$new = Fx::GroupBy($arr, array('func:gbFunc1', 'func:gbFunc2', 'func:gbFunc3'), array(null, null, 'func:vFunc3'));
print_r($new);