2013-05-19 66 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

見上文鱗翅目。 – 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

謝謝,但如何檢查$的帖子? – Bas

+1

爲您編輯,@Bas –

+0

謝謝,這就是我的解決方案! – Bas