У меня есть 2 массива, и я хочу создать выходной массив.Сравнение массива php foreach loop
Пример требований массива для заголовка и подзаголовка поля:
Array
(
[title] => Array
(
[required] => 1
[minLength] => 2
[maxLength] => 50
)
[subtitle] => Array
(
[required] => 1
[minLength] => 2
[maxLength] => 55
)
)
Сообщение массив после поста s:
Array
(
[title] =>
[subtitle] => s
)
Примера Выходного массив:
Array
(
[title] => Array
(
[0] => this field is required
[1] => must be longer than 2
)
[subtitle] => Array
(
[0] => must be longer than 2
)
)
Как я могу генерировать такой массив по петле foreach?
Это то, что у меня есть, но оно не будет работать хорошо. Если я оставлю заголовок пустым и символ подзаголовка 1, он возвращает 2 раза это поле. Похоже, он дублировал.
class Forms_FormValidationFields {
private $_required;
private $_minLength;
private $_maxLength;
private $_alphaNumeric;
public $_errors;
public function __construct($validate, $posts) {
array_pop($posts);
$posts = array_slice($posts,1);
foreach ($posts as $postName => $postValue) {
foreach($validate[$postName] as $key => $ruleValue){
$set = 'set'.ucfirst($key);
$get = 'get'.ucfirst($key);
$this->$set($postValue , $ruleValue);
if($this->$get() != '' || $this->$get() != NULL) {
$test[$postName][] .= $this->$get();
}
}
}
$this->_errors = $test;
}
public function setValidation(){
return $this->_errors;
}
public function getRequired() {
return $this->_required;
}
public function setRequired($value, $ruleValue) {
if (empty($value) && $ruleValue == TRUE) {
$this->_required = 'this field is required';
}
}
public function getMinLength() {
return $this->_minLength;
}
public function setMinLength($value, $ruleValue) {
if (strlen($value) < $ruleValue) {
$this->_minLength = ' must be longer than' . $ruleValue . '';
}
}
public function getMaxLength() {
return $this->_maxLength;
}
public function setMaxLength($value, $ruleValue) {
if (strlen($value) > $ruleValue) {
$this->_maxLength = 'must be shorter than' . $ruleValue . '';
}
}
}
Что делать в годовом сопоставлении до сих пор? Где вы заблокированы? – Lepidosteus
см. Выше Lepidosteus. – Bas