I want to combine the three arrays shown below and I don't know how to do that, I want to take the companyCode
of each array and make it for one array.
I have an array like this,
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'
)
)
)
I want this:
Array(
[AD] => Array(
[NYCT01] => Array(
[ECAR] => Array(
[0] => '295.62'
)
[SCAR] => Array(
[0] => '356.25'
)
[PCAR] => Array(
[0] => '562.36'
)
)
)
)
How can I do that, I tried to do some loops but I'm getting no result please help.
Here's a function I created to group arrays a couple of days ago:
for PHP 5.3 and above
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 example
$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);
for PHP 5.2 and possibly under
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 example
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);