无法在PHP中创建多维数组

时间:2016-02-22 17:16:11

标签: php arrays

我有一些名为$workers的工作人员,以及名为$jobs的工作。让我们每天都可以做$jobs_for_each个。工作现在我需要为日常工作创建一个数组。这是我的代码:

$all_workers=array("worker1","worker2","worker3","worker4", ... ... );
$all_jobs=array("j1","j2","j3", .... .... );
$jobs_for_each=7;
$k=0;
$day=0;
for ($n=0; $n < 3; $n++) 
{ 
   for ($j=0; $j < count($all_workers) ; $j++) 
   {
      for ($i=0; $i < $jobs_for_each; $i++) 
      {
         $job_arr[$day][trim($all_workers[$j])][]=trim($all_jobs[($k*$jobs_for_each)+$i]);
         $distributed_arr[]=trim($all_jobs[($k*$jobs_for_each)+$i]);
      }
      $k++; 
   }
   $remaining=array_diff ( $all_jobs , $distributed_arr );
   unset($all_jobs);
   $all_jobs = $remaining;
   if (empty($all_jobs))
   {
      $n=5;
   }
   else
   {
      $n=0;
      array_values($all_jobs);
   }
   $k=0;
   $day++;
}

此代码无效。我需要在$job_arr

的格式中$job_arr[day][worker]=array(jobs);

如果我的工作人员4且工作100每天工作7 gnuplot> print 100 ** 4 100000000 gnuplot> print 100 ** 5 1410065408 gnuplot> print 100 ** 6 -727379968 然后它需要4天,有些工人不应该在最后一天找到工作;

感谢。

1 个答案:

答案 0 :(得分:1)

我在第二部分做了一些更改并添加了isset:

element.Cast<Object>().Where(key=>((Convert.ToString(key).StartsWith("[") 
 && Convert.ToString(key).EndsWith("]"))
 &&(!((Convert.ToString(key+1).Contains("MD")
 ||(Convert.ToString(key+1).Contains("M"))))))

将输出:

    <?php
    $all_workers=array("worker1","worker2","worker3","worker4");
    $count_workers = count($all_workers);
    $all_jobs=array("j1","j2","j3","j4","j5","j6","j7","j8","j9","j10");
    $jobs_for_each=2;
    $k=0;
    $day=0;
    for ($n=0; $n < 3; $n++) 
    { 
        for ($j=0; $j < $count_workers ; $j++) 
        {
              for ($i=0; $i < $jobs_for_each; $i++) 
              {
                if(!isset($job_arr[$day])){
                    $job_arr[$day]=array();
                }

                 $job_left = count($all_jobs);
                  if( $job_left <= $jobs_for_each*$count_workers){
                      $jobs_for_each = ceil($job_left / $count_workers);
                  }
                 if(!isset($all_jobs[($k*$jobs_for_each)+$i])){
                    echo 'no more job<br />';break(3);
                 }else{
                     $job_arr[$day][trim($all_workers[$j])][]=trim($all_jobs[($k*$jobs_for_each)+$i]);
                     $distributed_arr[]=trim($all_jobs[($k*$jobs_for_each)+$i]);
                 }
              }
              $k++; 
        }


        $remaining=array_diff ( $all_jobs , $distributed_arr );
        if (empty($remaining))
        {
            break;
        }
        else
        {
            $all_jobs = array_values($remaining);
        }
        $k=0;
        $day++;
    }
    ?>