dsjuimtq920056 2012-06-10 21:22
浏览 35
已采纳

如何组合这个数组PHP [关闭]

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.

  • 写回答

1条回答 默认 最新

  • doubeijian2257 2012-06-11 00:49
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法