0

I need some help with re-working the logic on this php code. What I'd like to do is build in some conditions in this foreach loop to handle empty values, null and give it some default value like 'n/a'. Its using the $class_array to match one of the cases which is read from a text file, but if I don't have either of the statuses, then make it 'n/a'.

I'm just not clear how to construct that. Here is part of the code. I'm thinking, it must be handled within this foreach condition?? Thanks.

$class_array = array(
        'Process succeeded'      => 'success',
        'Process failed'         => 'failure',
        'Review Logs for status' => 'warn',
        'Check for Errors'       => 'warn'
);

foreach ($InputFile as $line){
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);
    $LineData = array();
    foreach ($matches as $information) {
        $LineData[$information[2]] = $information[3];
    }
    $timestamp = strtotime($LineData["LogDate"]." ".$LineData["StartTime"]);
    $LineData['StartTime'] = date("Y-m-d H:i:s",strtotime($LineData['StartTime']));
    $LineData['server_url'] = $server_array[$LineData['Server']];
    $LineData['status_class'] = $class_array[$LineData['Status']];

    $data[$timestamp] = $LineData;
}
3
  • 5
    Your example code is too noisy. Trim it down to the minimum necessary to understand your question. Commented Jan 10, 2011 at 22:28
  • are you trying to make your status_class = "n/a" or are you trying to make $data[$timestamp] = "n/a"? Commented Jan 10, 2011 at 22:51
  • 'status_class' = 'n/a' if none of the conditons for $class_array is met. Commented Jan 10, 2011 at 22:53

3 Answers 3

1

Do you mean that $LineData['status_class'] should be set to 'n/a' in case there is no $LineData['Status'] key in $class_array? If so, this should do:

$status_class = 'n/a';
if (array_key_exists($LineData['Status'], $class_array)) {
    $status_class = $class_array[$LineData['Status']];
}
$LineData['status_class'] = $status_class;
Sign up to request clarification or add additional context in comments.

3 Comments

right. basically, if 'Status' has no value read in from the input file. Embed this in the foreach?
Then just add "if (empty($LineData['Status'])) continue;" after the nestled foreach loop.
This will skip the current iteration and switch to the next line processing.
0

Use a shorthand conditional assignment.

//Lets assume we're getting some data from a form
$OriginalValue = $_POST['some_user_input'];

//Set a new variable, $NewValue, to the entered data (if available) or a default value.
$NewValue = ( empty($OriginalValue) ) ? 'No data entered' : $OriginalValue;

Simply put any condition (or set of conditions) within the parentheses, and this acts like an if/else. If true, set $NewValue to 'No data entered', else set $NewValue to $OriginalValue.

Comments

0

I think this should work:

if(isset($LineData['status_class'])) {

  $LineData['status_class'] array_key_exists($LineData['Status'], $class_array)
     ? $class_array[$LineData['Status']]
     : 'n/a';
}
else
{
  $LineData['status_class'] = 'n/a';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.