-3

以下に示す3つの配列を組み合わせたいのですが、その方法がわかりませんcompanyCode。各配列を1つの配列にまとめたいと思います。

私はこのような配列を持っています、

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'
                          )
                 )
        )
)

どうすればそれを行うことができますか、私はいくつかのループを実行しようとしましたが、結果が得られません。助けてください。

4

1 に答える 1

1

数日前に配列をグループ化するために作成した関数は次のとおりです。

PHP5.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);

PHP5.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);
于 2012-06-11T00:49:12.557 に答える