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

@グラハムの解決策は正しいです、

しかし、見苦しい回避策は、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 に答える