Need help with if Statement

Hi
I am a beginner with php.

$txn = array(
“TP_DEPT_GROUP” => “003”,
“TP_CODE” => “000000001025699485”,
“JOB_IDENT” => " ",
);

what i need to do is if Job_Ident is = to " " then concatenate TP_Dept_group & TP_Code (TP_Charge Code without the leading zeros) otherwise return Job_Ident. If unable to determine Job_Ident or TP_Code then report error" I hope this makes sense.

Thanks

Let’s use a ternary op.

[php]
return (!trim($txn[“JOB_IDENT”])) ? $txn[“TP_DEPT_GROUP”].ltrim($txn[“TP_CODE”],“0”) : $txn[“JOB_IDENT”];[/php]

Hopefully that’s what you are looking for. It will:

  • Check if JOB_IDENT is spaces (trim strips them. If it is only spaces, the result will be empty)
    • If it is empty, concat TP_DEPT_GROUP and TP_CODE without the leading zeros (done using ltrim with char key “0”)
    • Otherwise, return JOB_IDENT.

Fiddle: http://codepad.viper-7.com/aKiW0f

Thank you for the quick response. That works great.
Is there is a way to return an error if all 3 fields are blank.

Thanks

Before the return call:

[php]
$f = false;
foreach ($txn as $v) {
if (trim($v)) { $f = true; break; }
}
if (!$f) throw new Exception(“All fields are empty”);[/php]

Fiddle: http://codepad.viper-7.com/enxeiI . The error comes as an Exception and therefore needs a try/catch block.

Thanks
this looks like it will check all fields in the Array i only need to check TP_Dept_Group, TP_Code and Job_ident.
We will be adding more fields in the array such as description but do not want those to be checked by this code.

Thanks again

Okay; I couldn’t have known that :smiley:

Instead of hardcoding, consider this option:

[php]$fields = array(“TP_DEPT_GROUP”,“JOB_IDENT”,“TP_CODE”);
$f = false;
foreach (array_intersect_key($txn, array_flip($fields)) as $v) {
if (trim($v)) { $f = true; break; }
}
if (!$f) throw new Exception(“All fields are empty”);[/php]

This one is a bit arcane and will require some explanations:
array_flip turns all keys into values, and values into keys, of an array
array_intersect_key will return an array subset of the first argument (here $txn), where the remaining elements have their key present in all the other arguments.

Fiddle: http://codepad.viper-7.com/xGchZN

Thank you very much this is great.

sorry but i am getting the following error

Fatal error: Call to undefined function array_intersect_keys() in X:\test.php on line 7

Here is the full code

<?php function get_code($txn) { $fields = array("TP_DEPT_GROUP","JOB_IDENT","TP_CHARGE_CODE"); $f = false; foreach (array_intersect_keys($txn, array_flip($fields)) as $v) { if (trim($v)) { $f = true; break; } } if (!$f) throw new Exception("All fields are empty"); return (!trim($txn["JOB_IDENT"])) ? ltrim($txn["TP_DEPT_GROUP"],"0").ltrim($txn["TP_CHARGE_CODE"],"0") : $txn["JOB_IDENT"]; } try { #Vary putting a value in JOB_IDENT or ' ' to test $txn = array( "TP_DEPT_GROUP" => "003", "TP_CHARGE_CODE" => "000000001025699485", "JOB_IDENT" => " ", ); $code = get_code($txn); echo "Code is correct"; } catch (Exception $e) { echo "ERROR: " . $e->getMessage() ."\n"; exit(1); } ?>

thanks

My fault, it’s a typo - it’s array_intersect_key.

That did the trick.

Thanks again for the help, it is very much appreciated

Sponsor our Newsletter | Privacy Policy | Terms of Service