PHP两个阵列到一个表中。 First Array Vertical&横

时间:2016-04-06 08:34:49

标签: php arrays

data


    $salesman = array (1=>'James',2=>'Linda',3=>'Jorge',4=>'Abudi');


    $sales = array(
      '0' => array ( 
        'salesman' => '2',
        'retail_date' => '2016-01-01', 
      ), 
      '1' => array (
        'salesman' => '3',
        'retail_date' => '2016-01-01', 
      ), 
      '2' => array (
        'salesman' => '4',
        'retail_date' => '2016-01-15', 
      ), 
      '3' => array ( 
        'salesman' => '1',
        'retail_date' => '2016-01-15',  
      ), 
      '4' => array (
        'salesman' => '3',
        'retail_date' => '2016-01-01', 
      ),
      '5' => array (
        'salesman' => '3',
        'retail_date' => '2016-01-15', 
      ), 
      '6' => array ( 
        'salesman' => '2',
        'retail_date' => '2016-01-15',  
      ), 
      '7' => array (
        'salesman' => '4',
        'retail_date' => '2016-01-01', 
      ),
      '8' => array (
        'salesman' => '3',
        'retail_date' => '2016-01-15', 
      ), 
      '9' => array ( 
        'puskesmas_id' => '2',
        'retail_date' => '2016-01-15',  
      ), 
      '10' => array (
        'salesman' => '2',
        'retail_date' => '2016-01-01', 
      ),
      '11' => array (
        'salesman' => '3',
        'retail_date' => '2016-01-15', 
      ), 
      '12' => array ( 
        'salesman' => '4',
        'retail_date' => '2016-01-15',  
      ),
    );

                $injan = 0;
                $infeb = 0;
                $inmar = 0;
                $inapr = 0;
                $inmei = 0;
                $injun = 0;
                $injul = 0;
                $inaug = 0;
                $insep = 0;
                $inokt = 0;
                $innov = 0;
                $indes = 0;

          foreach($sales as $key => $val) {
    //          echo '
'; $retail_date = $val['retail_date']; $timestamp = strtotime($retail_date); $month = date("m", $timestamp); // echo $month; if ($month =='01') { $injan++; } else if ($month =='02'){ $infeb++; } else if ($month =='03'){ $inmar++; } else if ($month =='04'){ $inapr++; } else if ($month =='05'){ $inmei++; } else if ($month =='06'){ $injun++; } else if ($month =='07'){ $injul++; } else if ($month =='08'){ $inaug++; } else if ($month =='09'){ $insep++; } else if ($month =='10'){ $inokt++; } else if ($month =='11'){ $innov++; } } echo ''; echo 'SalesmanJanuariFebruariMaret'; echo 'April'; echo 'Mei'; echo 'June'; echo 'July'; echo 'August'; echo 'September'; echo 'October'; echo 'November'; echo 'Desember'; echo ''; echo 'total'.$injan.''; echo ''.$infeb.''; echo ''.$inmar.''; echo ''.$inapr.''; echo ''.$injun.''; echo ''.$injul.''; echo ''.$inaug.''; echo ''.$insep.''; echo ''.$inokt.''; echo ''.$innov.''; echo ''.$indes.''; echo ''; echo '';

我只能这样显示。

enter image description here

我想看看

enter image description here

3 个答案:

答案 0 :(得分:0)

这将为您提供按年份和月份分类的销售员名称。从那里你可以生成你的表:

$sales_by_date = array();

foreach ($sales as $sale) {
    $date = date_parse_from_format('Y-d-m', $sale['retail_date']);

    if (!array_key_exists($date['year'], $sales_by_date)) {
        $sales_by_date[$date['year']] = array();
    }

    if (!array_key_exists($date['month'], $sales_by_date[$date['year']])) {
        $sales_by_date[$date['year']][$date['month']] = array();
    }

    $sale['salesman_name'] = array_key_exists($sale['salesman'], $salesman) ? $salesman[$sale['salesman']] : false;

    $sales_by_date[$date['year']][$date['month']][] = $sale;
}

答案 1 :(得分:0)

首先获取所有销售人员数据,然后获取所有销售数据并检查其月份。 然后显示整个月。

 <table>
  <tr>
    <th>&nbsp;</th>
    <th>January</th>
    <th>February</th>
    <th>March</th>
    <th>April</th>
    <th>May</th>
    <th>June</th>
    <th>July</th>
    <th>August</th>
    <th>September</th>
    <th>October</th>
    <th>November</th>
    <th>December</th>
  </tr>
<?php 
  foreach ($salesman as $skey => $salesPerson ) 
  {
      $injan = 0;
      $infeb = 0;
      $inmar = 0;
      $inapr = 0;
      $inmei = 0;
      $injun = 0;
      $injul = 0;
      $inaug = 0;
      $insep = 0;
      $inokt = 0;
      $innov = 0;
      $indes = 0;
      foreach($sales as $key => $val) 
      {
          $retail_date = $val['retail_date'];
          $timestamp = strtotime($retail_date);
          $month = date("m", $timestamp);

          $salesman_id = !empty( $val['salesman'] ) ? $val['salesman'] : 0;
          // Check Salesman
          if ( $skey == $salesman_id )
          {
              if ($month =='01') {
                  $injan++;
              } else if ($month =='02'){
                  $infeb++;
              } else if ($month =='03'){
                  $inmar++;
              } else if ($month =='04'){
                  $inapr++;
              } else if ($month =='05'){
                  $inmei++;
              } else if ($month =='06'){
                  $injun++;
              } else if ($month =='07'){
                  $injul++;
              } else if ($month =='08'){
                  $inaug++;
              } else if ($month =='09'){
                  $insep++;
              } else if ($month =='10'){
                  $inokt++;
              } else if ($month =='11'){
                  $innov++;
              }else if ($month =='12'){
                  $indes++;
              }
          }                  
      }
       ?>
      <tr>
        <th><?php echo $salesPerson;?></th>
        <th><?php echo $injan;?></th>
        <th><?php echo $infeb;?></th>
        <th><?php echo $inmar;?></th>
        <th><?php echo $inapr;?></th>
        <th><?php echo $inmei;?></th>
        <th><?php echo $injun;?></th>
        <th><?php echo $injul;?></th>
        <th><?php echo $inaug;?></th>
        <th><?php echo $insep;?></th>
        <th><?php echo $inokt;?></th>
        <th><?php echo $innov;?></th>
        <th><?php echo $indes;?></th>
      </tr>
      <?php 
  }  
  ?>
</table>

答案 2 :(得分:0)

您也可以使用它。

<?php 

$salesman = array (1=>'James',2=>'Linda',3=>'Jorge',4=>'Abudi');


$sales = array(
  '0' => array ( 
    'salesman' => '2',
    'retail_date' => '2016-01-01', 
  ), 
  '1' => array (
    'salesman' => '3',
    'retail_date' => '2016-01-01', 
  ), 
  '2' => array (
    'salesman' => '4',
    'retail_date' => '2016-01-15', 
  ), 
  '3' => array ( 
    'salesman' => '1',
    'retail_date' => '2016-01-15',  
  ), 
  '4' => array (
    'salesman' => '3',
    'retail_date' => '2016-01-01', 
  ),
  '5' => array (
    'salesman' => '3',
    'retail_date' => '2016-01-15', 
  ), 
  '6' => array ( 
    'salesman' => '2',
    'retail_date' => '2016-01-15',  
  ), 
  '7' => array (
    'salesman' => '4',
    'retail_date' => '2016-01-01', 
  ),
  '8' => array (
    'salesman' => '3',
    'retail_date' => '2016-01-15', 
  ), 
  '9' => array ( 
    'salesman' => '2',
    'retail_date' => '2016-01-15',  
  ), 
  '10' => array (
    'salesman' => '2',
    'retail_date' => '2016-01-01', 
  ),
  '11' => array (
    'salesman' => '3',
    'retail_date' => '2016-01-15', 
  ), 
  '12' => array ( 
    'salesman' => '4',
    'retail_date' => '2016-01-15',  
  ),
);



$temp = array();

$year = array('01', '02', '03', '04', '05', '06', '07', '08' , '09', '10', '11', '12');

foreach ($salesman as $key => $man) {
    foreach ($sales as $sl => $value) { 

        if ($key == $value['salesman']) { 

            $retail_date = $value['retail_date'];
            $timestamp = strtotime($retail_date);
            $month = date("m", $timestamp);

            if (!empty($temp[$key][$month])) {
                $temp[$key][$month]++;
            } else {
                $temp[$key][$month] = 1;
            }
        }
    }
}

$finalRes = array();

foreach ($temp as $sales => $man) {
    foreach ($year as $key => $val) {
        $finalRes[$sales][$val] = (!empty($man[$val])) ? $man[$val] : 0;
    }
}


?>

<table width="100%" border="1">
    <tr>
        <th>&nbsp;</th>
        <th>January</th>
        <th>February</th>
        <th>March</th>
        <th>April</th>
        <th>May</th>
        <th>June</th>
        <th>July</th>
        <th>August</th>
        <th>September</th>
        <th>October</th>
        <th>November</th>
        <th>December</th>
    </tr>

    <?php foreach ($finalRes as $key => $val) { ?>

        <tr>
            <td><?php echo $salesman[$key];?></td>
            <?php foreach ($val as $v) { ?>
                <td><?php echo $v;?></td>
            <?php } ?>
        </tr>

    <?php } ?>
</table>