通过2个参数将数组排序到表

时间:2014-03-04 18:44:33

标签: php arrays

我有一个数组。我试着将它分组到这样的表格中。

-------------------------------------------------------------
          | 2014-01-21 | 2014-01-22 | 2014-01-23 | 2014-01-27 
-------------------------------------------------------------      
title1    |     50     |      5     |    20+7    |     0    |  
-------------------------------------------------------------
title2    |     40     |     20     |      0     |     0    |  
------------------------------------------------------------
title3    |      0     |      0     |     40     |     40   |  
-------------------------------------------------------------
title4    |      0     |      0     |      4     |     0    |  
-------------------------------------------------------------

即。我想按“标题”和“日期”分组到表格,并按第三个参数加总。

$items_arr =(
[0] => Array(  [1] => title1   [2] => 2014-01-21   [3] => 50)
[1] => Array(  [1] => title1   [2] => 2014-01-22   [3] => 5)
[2] => Array(  [1] => title1   [2] => 2014-01-23   [3] => 7)
[3] => Array(  [1] => title1   [2] => 2014-01-23   [3] => 20)
[4] => Array(  [1] => title2   [2] => 2014-01-21   [3] => 40)
[5] => Array(  [1] => title2   [2] => 2014-01-22   [3] => 20)
[6] => Array(  [1] => title3   [2] => 2014-01-23   [3] => 40)
[7] => Array(  [1] => title3   [2] => 2014-01-27   [3] => 40)
[8] => Array(  [1] => title4   [2] => 2014-01-23   [3] => 4)
)


for($i=0; $i<($numworks); $i++)
{ 
    echo "<tr><td>". $works_arr[$i]."</td>"; 
    for($j=0; $j<$numitems; $j++){ 
      if (($works_arr[$i] == $items_arr[$j][1])){ 
        echo "<td>".$items_arr[$j][3]."</td>"; 
      }else{ echo "<td>0</td>"; } 
     }; 
     echo "</tr>"; 
 }

$ numitems是$ items_arr的数字元素。

$numworks at  $works_arr Array([0] => title1 [1] => title2 [2] => title3 [3] => title4)

1 个答案:

答案 0 :(得分:1)

$items_arr = Array(
  Array(  1 => "title1",   2 => "2014-01-21",   3 => 50),
  Array(  1 => "title1",   2 => "2014-01-22",   3 => 5),
  Array(  1 => "title1",   2 => "2014-01-23",   3 => 7),
  Array(  1 => "title1",   2 => "2014-01-23",   3 => 20),
  Array(  1 => "title2",   2 => "2014-01-21",   3 => 40),
  Array(  1 => "title2",   2 => "2014-01-22",   3 => 20),
  Array(  1 => "title3",   2 => "2014-01-23",   3 => 40),
  Array(  1 => "title3",   2 => "2014-01-27",   3 => 40),
  Array(  1 => "title4",   2 => "2014-01-23",   3 => 4),
);
$rows = Array();
$cols = Array();
$assoc_array = Array();

// create a assoc array
foreach($items_arr as $item) {
  if(isset($assoc_array[$item[1]]) AND isset($assoc_array[$item[1]][$item[2]]))
    $assoc_array[$item[1]][$item[2]] += $item[3];
  else
    $assoc_array[$item[1]][$item[2]] = $item[3];
  $rows[] = $item[1];
  $cols[] = $item[2];
}

// sort the cols and rows
$cols = array_unique($cols);
$rows = array_unique($rows);

//print the table
print "<tr><td></td>";
foreach($cols as $col) {
  print "<td>{$col}</td>";
}
print "</tr>";
foreach($rows as $row) {
  print "<tr><td>{$row}</td>";
  foreach($cols as $col) {
    print "<td>";
    if(isset($assoc_array[$row]) AND isset($assoc_array[$row][$col]))
      print $assoc_array[$row][$col];
    else
      print "0";
    print "</td>";
  }
  print "</tr>";
}

老答案:

函数usort有助于为特殊问题创建排序算法。

$items_arr = Array(
  Array(  1 => "title1",   2 => "2014-01-21",   3 => 50),
  Array(  1 => "title1",   2 => "2014-01-22",   3 => 5),
  Array(  1 => "title1",   2 => "2014-01-23",   3 => 7),
  Array(  1 => "title1",   2 => "2014-01-23",   3 => 20),
  Array(  1 => "title2",   2 => "2014-01-21",   3 => 40),
  Array(  1 => "title2",   2 => "2014-01-22",   3 => 20),
  Array(  1 => "title3",   2 => "2014-01-23",   3 => 40),
  Array(  1 => "title3",   2 => "2014-01-27",   3 => 40),
  Array(  1 => "title4",   2 => "2014-01-23",   3 => 4),
);

function specialsort(a,b) {
  if ($a[1] == $b[1]) {
    if ($a[3] == $b[3]) {
      return 0;
    }
    return ($a[3] < $b[3]) ? -1 : 1;
  }
  return ($a[1] < $b[1]) ? -1 : 1;
}

usort($items_arr, "specialsort");