auicsc
auicsc

Reputation: 297

Retrieve values from php array

So I have the following array that is generated from a $_POST after making some unset() modifications:

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 ) 

I run the following code to extract the values and display them on form of:

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

The code:

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

But what I get is with the 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

Upvotes: 1

Views: 150

Answers (3)

Graham Walters
Graham Walters

Reputation: 2064

Simply declaring all of your variables at the top would eliminate this error. I.E.

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

The reason being that sometimes you declare $action_quarter and $action_year, but not always because of your if statements.

I also believe your array_chunk($array,2,true) should read array_chunk($array,5,true).

Here is a working copy of your code:

<?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 />";
}
?>

NOTE I have shortened your array, simply because I couldn't be bothered to type it all.

Upvotes: 2

HamZa
HamZa

Reputation: 14921

No need to use such a complicated code, try the following:

$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

Online demo.


Generating a query:

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

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

Upvotes: 1

d.raev
d.raev

Reputation: 9546

@Graham solution is correct,

But a ugly workaround is just to change the error_reporting level and hide Warning / Notice messages.

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

Put the code above on top of your php code or in index.php .

As I said it is not good practice to "hide" the warnings, but if you are debugging a legacy project or for any reason can not afford to fix all the warnings ... it may be useful :)

Upvotes: 0

Related Questions