2013-05-19 3 views
0

나는 2 배열을 가지고 출력 배열을 만들고 싶습니다.배열을 비교 PHP foreach 루프

제목과 자막 필드

예 배열 요구 사항 :

Array 
(
[title] => Array 
    (
     [required] => 1 
     [minLength] => 2 
     [maxLength] => 50 
    ) 

[subtitle] => Array 
    (
     [required] => 1 
     [minLength] => 2 
     [maxLength] => 55 
    ) 

) 

포스트 포스트의 후 배열 :

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 . ''; 
    } 
} 

} 
+1

지금까지 있나요? 어디에서 차단 되었습니까? – Lepidosteus

+0

위의 Lepidosteus 참조. – Bas

답변

2

당신이 이동 : 당신이 위에 읽으면

<?php 
    $required = array(
     'This field is not required', 
     'This field is required' 
    ); 

    $length = 'Requires more than {less} but less than {more}'; 

    $needs = array(
     'title' => array(
      'required' => 1, 
      'minLength' => 2, 
      'maxLength' => 50, 
     ), 

     'subtitle' => array(
      'required' => 1, 
      'minLength' => 2, 
      'maxLength' => 55 
     ) 
    ); 

    $new_needs = array(); 

    foreach($needs as $key => $value) // Loop over your array 
    { 
     $new_needs[$key][] = $required[$value['required']]; 
     $new_needs[$key][] = str_replace(
      array('{more}', '{less}'), 
      array($value['maxLength'], $value['minLength']), 
      $length 
     ); 
    } 

    foreach($_POST as $key => $value) 
    { 
     if(empty($value)) { echo $new_needs[$key][0]; } 

     if(strlen($value) > $needs[$key]['maxLength'] || strlen($value) < $needs[$key]['minLength']) echo $new_needs[$key][1]; 
    } 

꽤 자기 설명 작아야합니다.

결과 : 전년 동기 대비하는 일

Array 
(
    [title] => Array 
     (
      [0] => This field is required 
      [1] => Requires more than 2 but less than 50 
     ) 

    [subtitle] => Array 
     (
      [0] => This field is required 
      [1] => Requires more than 2 but less than 55 
     ) 

) 
+0

감사합니다.하지만 $ posts를 확인하는 방법은 무엇입니까? – Bas

+1

편집 됨, @Bas –

+0

고마워요, 저에게 해결책이 있네요! – Bas