1

因此,我$_POST在进行一些unset()修改后生成了以下数组:

Array ( [actual-1] => 2 [target-1] => 4 [act-1] => dzdz [quarter-1] => 3 [year-1] => 2016 [actual-2] => 1 [target-2] => 3 [act-2] => zz [quarter-2] => 2 [year-2] => 2016 [actual-53] => 3 [target-53] => 2 [act-53] => zzd [quarter-53] => 1 [year-53] => 2015 [actual-58] => 5 [target-58] => 1 [act-58] => eec [quarter-58] => 2 [year-58] => 2013 ) 

我运行以下代码来提取值并以以下形式显示它们:

ID -- Level -- Action -- Target -- Action Quarter: -- Action year -- 

编码:

foreach(array_chunk($array,2,true) as $val) {
  foreach($val as $k=>$v) {
    if (strpos($k, "actual") !== false) {
      $temp    = explode("-",$k);
      $id      = $temp[1];
      $actual  = $v;
    }
    if (strpos($k, "act") !== false) {
      $action = $v;
    }
    if (strpos($k,"target") !== false) {
      $target= $v;
    }
    if (strpos($k, "quarter") !== false) {
      $action_quarter = $v;
    }
    if(strpos($k, "year") !== false){
      $action_year = $v;
    }           
  }
  echo "ID ".$id." Level ".$actual." action ".$action." Target: ".$target. " Action Quarter: ". $action_quarter. " Action year : ".$action_year;
  echo "<br><--->";
}

但我得到的是output messing up the values

Notice: Undefined variable: action_quarter in C:\www\index\DevIT\classes\hr_competences.php on line 475

Notice: Undefined variable: action_year in C:\www\index\DevIT\classes\hr_competences.php on line 475
ID 1 Level 2 action 2 Target: 4 Action Quarter: Action year :
<--->
Notice: Undefined variable: action_year in C:\www\index\DevIT\classes\hr_competences.php on line 475
ID 1 Level 2 action dzdz Target: 4 Action Quarter: 3 Action year :
<--->ID 2 Level 1 action 1 Target: 4 Action Quarter: 3 Action year : 2016
<--->ID 2 Level 1 action zz Target: 3 Action Quarter: 3 Action year : 2016
<--->ID 2 Level 1 action zz Target: 3 Action Quarter: 2 Action year : 2016
<--->ID 53 Level 3 action 3 Target: 2 Action Quarter: 2 Action year : 2016
<--->ID 53 Level 3 action zzd Target: 2 Action Quarter: 1 Action year : 2016
<--->ID 58 Level 5 action 5 Target: 2 Action Quarter: 1 Action year : 2015
<--->ID 58 Level 5 action eec Target: 1 Action Quarter: 1 Action year : 2015
<--->ID 58 Level 5 action eec Target: 1 Action Quarter: 2 Action year : 2013
4

3 回答 3

2

只需在顶部声明所有变量即可消除此错误。IE

<?php
$id = null;
$actual = null;
$action = null;
$target = null;
$action_quarter = null;
$action_year = null;
// Your code

原因是有时您声明$action_quarterand $action_year,但并不总是因为您的 if 语句。

我也相信你array_chunk($array,2,true)应该阅读array_chunk($array,5,true)

这是您的代码的工作副本:

<?php
$id = null;
$actual = null;
$action = null;
$target = null;
$action_quarter = null;
$action_year = null;
$array = array('actual-1' => 2, 'target-1' => 4, 'act-1' => 'dzdz', 'quarter-1' => 3, 'year-1' => 2016, 'actual-2' => 2, 'target-2' => 4, 'act-2' => 'dzdz', 'quarter-2' => 3, 'year-2' => 2016);

foreach(array_chunk($array, 5, true) as $val) {
  foreach($val as $k=>$v) {
    if (strpos($k, "actual") !== false) {
      $temp    = explode("-",$k);
      $id      = $temp[1];
      $actual  = $v;
    }
    if (strpos($k, "act") !== false) {
      $action = $v;
    }
    if (strpos($k,"target") !== false) {
      $target= $v;
    }
    if (strpos($k, "quarter") !== false) {
      $action_quarter = $v;
    }
    if(strpos($k, "year") !== false){
      $action_year = $v;
    }           
  }
  echo "ID ".$id." Level ".$actual." action ".$action." Target: ".$target. " Action Quarter: ". $action_quarter. " Action year : ".$action_year."<br />";
}
?>

注意我已经缩短了你的数组,只是因为我懒得全部输入。

于 2013-04-16T08:39:45.253 回答
1

不用这么复杂的代码,试试下面的:

$array = array( 'actual-1' => 2, 'target-1' => 4, 'act-1' => 'dzdz', 'quarter-1' => 3, 'year-1' => 2016, 'actual-2' => 1, 'target-2' => 3, 'act-2' => 'zz', 'quarter-2' => 2, 'year-2' => 2016, 'actual-53' => 3, 'target-53' => 2, 'act-53' => 'zzd', 'quarter-53' => 1, 'year-53' => 2015, 'actual-58' => 5, 'target-58' => 1, 'act-58' => 'eec', 'quarter-58' => 2, 'year-58' => 2013 );

$new_array = array();
foreach($array as $k => $v){
    list($name, $n) = explode('-', $k);
    $new_array[$n][$name] = $v;
}

echo '<pre>';print_r($new_array);echo '</pre>'; // print the new Array

// Print a nice table ?
$table = '<table border="1"><tr><td>ID</td><td>Level</td><td>Action</td><td>Target</td><td>Action Quarter</td><td>Action Year</td></tr>';
foreach($new_array as $k => $v){
    $table .= '<tr><td>'.$k.'</td><td>'.implode('</td><td>', $v).'</td></tr>';
}
$table .= '</table>';

echo $table; // print the table

在线演示


生成查询:

$_SESSION['KayttajaId'] = 545;$employee = 'wut'; // for testing purposes

$query = 'INSERT INTO actual_levels(manager_id,employee_id,comp_id,actual_level,actiontext,time,theyea‌​r,target,year,quarter) VALUES ';

$c = count($new_array);$i=1;
foreach($new_array as $k => $v){
    // Maybe you want to change the manager ID & employee after each iteration ?
    $query .= '("'.$_SESSION['KayttajaId'].'","'.$employee.'","'.$k.'","'.$v['actual'].'","'.$v['target'].'",GetDate(), "2012","'.$v['act'].'","'.$v['year'].'","'.$v['quarter'].'")';
    if($i >= $c){break;}
    $i++;
    $query .= ', ';
}

echo $query;

询问:

INSERT INTO actual_levels(manager_id,employee_id,comp_id,actual_level,actiontext,time,theyea‌​r,target,year,quarter) VALUES 
("545","wut","1","2","4",GetDate(), "2012","dzdz","2016","3"), 
("545","wut","2","1","3",GetDate(), "2012","zz","2016","2"), 
("545","wut","53","3","2",GetDate(), "2012","zzd","2015","1"), 
("545","wut","58","5","1",GetDate(), "2012","eec","2013","2")
于 2013-04-16T08:43:20.173 回答
0

@Graham 解决方案是正确的,

但是一个丑陋的解决方法就是更改error_reporting 级别并隐藏警告/通知消息。

error_reporting(E_ERROR); // display only errors a.k.a crash
//error_reporting(E_WARNING); display only errors, warning
//error_reporting(E_ALL); // display only errors, warning, notice, deprecated .. etc

ini_set('display_errors', '1');  

将上面的代码放在您的 php 代码或index.php.

正如我所说,“隐藏”警告不是一个好习惯,但如果您正在调试遗留项目或由于任何原因无法修复所有警告......它可能很有用:)

于 2013-04-16T08:48:12.277 回答