在数组中分组

时间:2016-09-07 06:56:08

标签: php

我有以下数组..我正在尝试对此进行分组。

  Array
   (
     [0] => Array
    (
        [Title] => HoMedics MAN-300
        [ean] => 31262006288
        [upc] => 31262006288
        [ProductImageName] => 
        [CdnUri] => 
        [ASIN] => B000050FEU
        [ListPrice] => 129.99
        [Status] => 2
        [ActualPrice] => 129.99
        [ProductID] => 5286728
    )

 )

我想把它归为

Array
(
[0] => Array
    (
        [VitalInfo]=>array(
            [Title] => HoMedics MAN-300 
            [ean] => 31262006288
            [upc] => 31262006288
            [ProductImageName] => 
            [CdnUri] => 
            [ASIN] => B000050FEU
       )
       [Price]=>array(
             [ListPrice] => 129.99
            [Status] => 2
            [ActualPrice] => 129.99
            [ProductID] => 5286728
        )
   )

我试过但它并没有像我想的那样发生......任何帮助都会很棒......坦克提前......

4 个答案:

答案 0 :(得分:1)

  

只需循环遍历数组并相应地自定义新数组。

假设$ array是原始数组,$ result是自定义数组,请尝试:

foreach ($array as $k => $arr) {

    $result[$k]['VitalInfo'] = array(
        'Title'            =>  $arr['Title'],
        'ean'              =>  $arr['ean'],
        'upc'              =>  $arr['upc'],
        'ProductImageName' =>  $arr['ProductImageName'],
        'CdnUri'           =>  $arr['CdnUri'],
        'ASIN'             =>  $arr['ASIN']
    );

    $result[$k]['Price'] = array(
        'ListPrice'   =>  $arr['ListPrice'],
        'Status'      =>  $arr['Status'],
        'ActualPrice' =>  $arr['ActualPrice'],
        'ProductID'   =>  $arr['ProductID']        
    );

 }

答案 1 :(得分:1)

试试这个, 代码

foreach($old_array as $key_old => $val_old)
 {
    foreach($val_old as $key => $val)
    {
        if(in_array($key, $VitalInfo_array))
        {
            $new_array[$key_old]['VitalInfo'][$key] = $val;
        }
        else
        {
            $new_array[$key_old]['Price'][$key] = $val;
        }
    }
 }

输出

Array
(
    [0] => Array
        (
            [VitalInfo] => Array
                (
                    [Title] => HoMedics MAN-300
                    [ean] => 31262006288
                    [upc] => 31262006288
                    [ProductImageName] => 
                    [CdnUri] => 
                    [ASIN] => B000050FEU
                )

            [Price] => Array
                (
                    [ListPrice] => 129.99
                    [Status] => 2
                    [ActualPrice] => 129.99
                    [ProductID] => 5286728
                )

        )

)

<强> DEMO

我希望它会有所帮助。

答案 2 :(得分:0)

如果您要在php中对此进行分组,请查看these answers

或者只是复制一下:

$input = [0 => [
    'Title' => 'HoMedics MAN-300',
    'ean' => 31262006288,
    'upc' => 31262006288,
    'ProductImageName' => '',
    'CdnUri' => '',
    'ASIN' => 'B000050FEU',
    'ListPrice' => 129.99,
    'Status' => 2,
    'ActualPrice' => 129.99,
    'ProductID' => 5286728
]];
foreach ($input as $in){
    $out['VitalInfo'] = [];
    $out['Price'] = [];
    foreach ($in as $key => $i){
        if (in_array($key, ['Title', 'ean', 'upc', 'ProductImageName', 'CdnUri', 'Asin'])){
            $out['VitalInfo'][] = [$key => $i];
        } else {
            $out['Price'][] = [$key => $i];
        }
    }
    $output[]=$out;
}
echo '<pre>';
var_dump($output);

答案 3 :(得分:0)

输入:$ info //您的原始数组 输出:$ finalArr //您需要的数组

 $vitalInfo = array ('Title','ean','upc','ProductImageName','CdnUri','ASIN');
 $price = array ('ListPrice','Status','ActualPrice','ProductId');    
 $finalArr = array();

 foreach ($info as $arr) {
   $result = array();
   foreach($arr as $k => $v){
       if(in_array($k,$vitalInfo))
            $result['VitalInfo'][$k] = $v;
       else if(in_array($k,$price))
            $result['Price'][$k] = $v;
   }
   $finalArr[] = $result;
}