简体   繁体   English

PHP 将两个数组合并到一个表中。 第一个阵列垂直和水平

[英]PHP Two Arrays Into One Table. First Array Vertical & Horizontal

数据

$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 '';

I can only display like this.我只能这样显示。

在此处输入图片说明

I want to make this look我想让这个样子

在此处输入图片说明

This will give you an array broken down by year and months, with salesmen names.这将为您提供一个按年份和月份细分的数组,其中包含销售人员的姓名。 From that you can generate your table:从中你可以生成你的表:

$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;
}

At first fetch all salesman data after that fetch all sales data and check its month.首先获取所有销售员数据,然后获取所有销售数据并检查其月份。 Then show all month.然后显示整个月。

 <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>

You can use this also.你也可以使用这个。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM