如何对同一个数组进行分组

时间:2014-11-21 10:25:24

标签: php arrays grouping

示例我的数组

Array (
 [0] => Array
     (
         [product_name] => T-Shirt
         [product_id] => 231
         [user_id] => 22977
     )

 [1] => Array
     (
         [product_name] => Shirt
         [product_id] => 220
         [user_id] => 22977
     )

 [2] => Array
     (
         [product_name] => T-Shirt
         [product_id] => 226
         [user_id] => 16916
     )

 [3] => Array
     (
         [product_name] => Bags
         [product_id] => 230
         [user_id] => 16916
     )

 [4] => Array
     (
         [product_name] => Hats
         [product_id] => 233
         [user_id] => 22977
     )

如何生成此数组

User-Id: 22977
1/ Hats
2/ Shirt
3/ T-Shirt


User-Id: 16916
1/ Bags
2/ T-Shirt

3 个答案:

答案 0 :(得分:0)

您可以使用:

$testarray = array(
        array(
            "product_name" => 'T-Shirt', 
            "product_id" => 231, 
            "user_id" => 22977),
        array
            (
            "product_name" => 'Shirt',
            "product_id" => 220,
            "user_id" => 22977,
        ),
        array
            (
            "product_name" => 'T-Shirt',
            "product_id" => 226,
            "user_id" => 16916,
        ),
        array
            (
            "product_name" => 'Bags',
            "product_id" => 230,
            "user_id" => 16916,
        ),
        array
            (
            "product_name" => 'Hats',
            "product_id" => 233,
            "user_id" => 22977,
        ),
);
$newArray = array();

foreach ($testarray as $subArray) {
    $newArray[$subArray["user_id"]][] = $subArray['product_name'];
}
var_dump ($newArray);

输出是:

array
  22977 => 
    array
      0 => string 'T-Shirt' (length=7)
      1 => string 'Shirt' (length=5)
      2 => string 'Hats' (length=4)
  16916 => 
    array
      0 => string 'T-Shirt' (length=7)
      1 => string 'Bags' (length=4)

答案 1 :(得分:0)

<?php
    //initialize array
    $array = Array(
        '0' => Array 
            ( 
                'product_name' => 'T-Shirt', 
                'product_id' => 231, 
                'user_id' => 22977 
            ),

        '1' => Array
            (
                'product_name' => 'Shirt',
                'product_id' => 220,
                'user_id' => 22977
            ),

        '2' => Array
            (
                'product_name' => 'T-Shirt',
                'product_id' => 226,
                'user_id' => 16916
            ),

        '3' => Array
            (
                'product_name' => 'Bags',
                'product_id' => 230,
                'user_id' => 16916
            ),

        '4' => Array
            (
                'product_name' => 'Hats',
                'product_id' => 233,
                'user_id' => 22977
            )
    );

    //result will be here
    $result = array();

    foreach ($array as $key => $value) {
        //check if we have keys group or names to avoid errors
        if(!isset($value['user_id']) || !isset($value['product_name']))
            continue;

        //make a key in result array if its not exist
        if(!isset($result[$value['user_id']]))
        {
            $result[$value['user_id']] = array($value['product_name']);
        }
        else
        {
            //add a values to key if it exists
            $result[$value['user_id']][] = $value['product_name'];
            //filter same values
            $result[$value['user_id']] = array_values(array_unique($result[$value['user_id']]));
        }
    }
    echo '<pre>';
    print_r($result);
    echo '</pre>';
?>

答案 2 :(得分:0)

$a = array();
$a[] = array("product_name" => "T-Shirt", "product_id" => 231, "user_id" => 22977);
$a[] = array("product_name" => "Shirt", "product_id" => 220, "user_id" => 22977);
$a[] = array("product_name" => "T-Shirt", "product_id" => 226, "user_id" => 16916);
$a[] = array("product_name" => "Bags", "product_id" => 230, "user_id" => 16916);
$a[] = array("product_name" => "Hats", "product_id" => 233, "user_id" => 22977);

$return = array(); 

foreach ($a as $key => $value) {
  $return[$value["user_id"]][] = $value["product_name"];
}

foreach ($return as $key => $value) {
  echo "User-Id: " . $key . "\r\n";
  $i = 0;

  foreach ($value as $val) {
    echo ++$i . "/ " . $val . "\r\n";
  }
}

输出将是:

User-Id: 22977
1/ T-Shirt
2/ Shirt
3/ Hats
User-Id: 16916
1/ T-Shirt
2/ Bags