Reputation: 433
I am having a little issue when I try to receive multiple array values in PHP for validation. I have this object with values in POSTMAN raw form, application/json
:
{
"name":"create_insert_new_delivery",
"param":{
"email_of_shipment_owner":"[email protected]",
"shipment_type ":"B",
"item_name": ["50", "60"],
"item_weight ": ["H", "I"],
"item_length": ["70", "90"]
}
}
When I try to receive $item_name = $this->validateParameter('item_name', $this->param['item_name'], ARRAY, true); like the two strings below are received I get this
**ERROR : **
[04-Jul-2018 13:16:58 UTC] PHP Parse error: syntax error, unexpected ',', expecting '(' in /home/osconliz/public_html/Osconlizapicall/api.php on line 304
The receiving code below.
LINE 302
$email_of_shipment_owner = $this->validateParameter('email_of_shipment_owner', $this->param['email_of_shipment_owner'], STRING, true);
LINE 303
$shipment_type = $this->validateParameter('shipment_type', $this->param['shipment_type'], STRING, true);
LINE 304
$item_name = $this->validateParameter('item_name', $this->param['item_name'], ARRAY, true);
LINE 305
$item_weight = $this->validateParameter('item_weight', $this->param['item_weight'], ARRAY, true);
LINE 306
$item_length = $this->validateParameter('item_length', $this->param['item_length'], ARRAY, true);
but the STRINGS are received properly :
public function validateParameter($fieldName, $value, $dataType, $required = true){
switch ($dataType) {
case BOOLEAN:
if (!is_bool($value)){
$this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be boolean.');
}
break;
case INTEGER:
if (!is_numeric($value)){
$this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be integer.');
}
break;
case STRING:
if (!is_string($value)){
$this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be string.');
}
break;
case ARRAY:
if (!is_array($value)){
$this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be array.');
}
break;
default:
$this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName);
break;
}
return $value;
}
After receiving I want validate data like this
if ($this->item_category == ""){
} else {
if ($this->item_category == "A" || $this->item_category == "B" || $this->item_category == "C" || $this->item_category == "D" || $this->item_category == "E" || $this->item_category == "F" || $this->item_category == "G" || $this->item_category == "H") {
} else {
$this->throwError(INVALID_DATA_TTT, "Invalid item category");
exit();
}
}
Upvotes: 0
Views: 909
Reputation: 4135
This is because ARRAY
is an apparently case-insensitive function for creating arrays. See it here: https://3v4l.org/keu3K
The easiest way to fix this, is to just pick another keyword I guess?
EDIT - For reference, all the reserved keywords and functions in php: https://secure.php.net/manual/en/reserved.keywords.php
Upvotes: 3