所以我在进行了一些$_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
答案 0 :(得分:2)
只需在顶部声明所有变量即可消除此错误。即。
<?php
$id = null;
$actual = null;
$action = null;
$target = null;
$action_quarter = null;
$action_year = null;
// Your code
原因是有时你声明$action_quarter
和$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 />";
}
?>
注意我缩短了您的阵列,只是因为我无法打扰所有人。
答案 1 :(得分: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,theyear,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,theyear,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")
答案 2 :(得分:0)
@Graham解决方案是正确的,
但是一个丑陋的解决方法就是更改error_reporting level并隐藏警告/通知消息。
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
。
正如我所说,“隐藏”警告不是好习惯,但如果您正在调试遗留项目或因任何原因无法修复所有警告......它可能有用:)