How to group subarrays by a column value?

前端 未结 18 1773
一生所求
一生所求 2020-11-22 10:43

I have the following array

Array
(
    [0] => Array
        (
            [id] => 96
            [shipping_no] => 212755-1
            [part_no] =&         


        
相关标签:
18条回答
  • 2020-11-22 11:01

    I just threw this together, inspired by .NET LINQ

    <?php
    
    // callable type hint may be "closure" type hint instead, depending on php version
    function array_group_by(array $arr, callable $key_selector) {
      $result = array();
      foreach ($arr as $i) {
        $key = call_user_func($key_selector, $i);
        $result[$key][] = $i;
      }  
      return $result;
    }
    
     $data = array(
            array(1, "Andy", "PHP"),
            array(1, "Andy", "C#"),
            array(2, "Josh", "C#"),
            array(2, "Josh", "ASP"),
            array(1, "Andy", "SQL"),
            array(3, "Steve", "SQL"),
        );
    
    $grouped = array_group_by($data, function($i){  return $i[0]; });
    
    var_dump($grouped);
    
    ?>
    

    And voila you get

    array(3) {
      [1]=>
      array(3) {
        [0]=>
        array(3) {
          [0]=>
          int(1)
          [1]=>
          string(4) "Andy"
          [2]=>
          string(3) "PHP"
        }
        [1]=>
        array(3) {
          [0]=>
          int(1)
          [1]=>
          string(4) "Andy"
          [2]=>
          string(2) "C#"
        }
        [2]=>
        array(3) {
          [0]=>
          int(1)
          [1]=>
          string(4) "Andy"
          [2]=>
          string(3) "SQL"
        }
      }
      [2]=>
      array(2) {
        [0]=>
        array(3) {
          [0]=>
          int(2)
          [1]=>
          string(4) "Josh"
          [2]=>
          string(2) "C#"
        }
        [1]=>
        array(3) {
          [0]=>
          int(2)
          [1]=>
          string(4) "Josh"
          [2]=>
          string(3) "ASP"
        }
      }
      [3]=>
      array(1) {
        [0]=>
        array(3) {
          [0]=>
          int(3)
          [1]=>
          string(5) "Steve"
          [2]=>
          string(3) "SQL"
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 11:01

    Expanding on @baba's answer, which I like, but creates a more complex three level deep multi-dimensional (array(array(array))):

    $group = array();
     foreach ( $array as $value ) {
       $group[$value['id']][] = $value; 
     }
    
    // output only data from id 96
    foreach ($group as $key=>$value) { //outer loop
     foreach ($value as $k=>$v){ //inner loop
      if($key==96){ //if outer loop is equal to 96 (could be variable)
       for ($i=0;$i<count($k);$i++){ //iterate over the inner loop
            printf($key.' has a part no. of '.$v['part_no'].' and shipping no. of '.$v['shipping_no'].'<br>');
       }
     }
    }
     }
    

    Will output:

    96 has a part no. of reterty and shipping number of 212755-1

    96 has a part no. of dftgtryh and shipping number of 212755-1

    0 讨论(0)
  • 2020-11-22 11:02
    function groupeByPHP($array,$indexUnique,$assoGroup,$keepInOne){
    $retour = array();
    $id = $array[0][$indexUnique];
    foreach ($keepInOne as $keep){
        $retour[$id][$keep] = $array[0][$keep];
    }
    foreach ($assoGroup as $cle=>$arrayKey){
        $arrayGrouped = array();
            foreach ($array as $data){
                if($data[$indexUnique] != $id){
                    $id = $data[$indexUnique];
                    foreach ($keepInOne as $keep){
                        $retour[$id][$keep] = $data[$keep];
                    }
                }
                foreach ($arrayKey as $val){
                    $arrayGrouped[$val] = $data[$val];
                }
                $retour[$id][$cle][] = $arrayGrouped;
                $retour[$id][$cle] = array_unique($retour[$id][$cle],SORT_REGULAR);
            }
    }
    return  $retour;
    }
    

    Try this function

    groupeByPHP($yourArray,'id',array('desc'=>array('part_no','packaging_type')),array('id','shipping_no')) 
    
    0 讨论(0)
  • 2020-11-22 11:04

    This should group an associative array Ejm Group By Country

    function getGroupedArray($array, $keyFieldsToGroup) {   
        $newArray = array();
    
        foreach ($array as $record) 
            $newArray = getRecursiveArray($record, $keyFieldsToGroup, $newArray);
    
        return $newArray;
    }
    function getRecursiveArray($itemArray, $keys, $newArray) {
        if (count($keys) > 1) 
            $newArray[$itemArray[$keys[0]]] = getRecursiveArray($itemArray,    array_splice($keys, 1), $newArray[$itemArray[$keys[0]]]);
        else
            $newArray[$itemArray[$keys[0]]][] = $itemArray;
    
        return $newArray;
    }
    
    $countries = array(array('Country'=>'USA', 'State'=>'California'),
                       array('Country'=>'USA', 'State'=>'Alabama'),
                       array('Country'=>'BRA', 'State'=>'Sao Paulo'));
    
    $grouped = getGroupedArray($countries, array('Country'));
    
    0 讨论(0)
  • 2020-11-22 11:05
    for($i = 0 ; $i < count($arr)  ; $i++ )
    {
        $tmpArr[$arr[$i]['id']] = $arr[$i]['id'];
    }
    $vmpArr = array_keys($tmpArr);
    print_r($vmpArr);
    
    0 讨论(0)
  • 2020-11-22 11:05

    I think this works better in PHP 5.5+

    $IdVar = array_column($data, 'id');
    
    0 讨论(0)
提交回复
热议问题