簡體   English   中英

PHP比較數組值以進行驗證

[英]PHP Compare Array Values for Validation

好的,我有一個非常個性化的問題,請耐心等待。

我基本上有兩套數據,我想將它們與許多不同的可能性進行比較。

$data  = array(
             'object'=>'ball', // Should check VALID (Rule 2)
             'color'=>'white', // VALID (Rule 2)
             'heavy'=>'no',    // VALID (Rule 1)
             'name'=>'wilson', // VALID (Rule 5)
             'funny'=>'no'     // INVALID (Rule 4)
              );

$data_2 = array(
             'object'=>'box',   // VALID (Rule 2)
             'color'=> 'blue',  // VALID (Rule 2)
             'texture'=>'hard', // VALID (Rule 1)
             'heavy'=>'yes',    // INVALID (Rule 4)
             'stupid'=>'no'     // INVALID (Rule 4)
                                // Name is INVALID because it is missing (Rule 3)

);

$required = array(
             'color'=>array('white','blue'),
             'heavy'=> 'no',
             'name'
);

$errors = array(
         'color'=>array('required'=>'Color is Required','invalid'=>'Color invalid')
         'object'=>array('invalid'=>'Object invalid'),
         'texture'=>array('invalid'=>'Texture invalid'),
         'heavy'=>array('required'=>'Heavy is Required','invalid'=>'Heavy invalid'),
         'name'=>array('required'=>'Name is Required','max_char'=>'Name exceeds char limit',
         'invalid'=>'Invalid item provided',          
);

$blueprint = array(
                 'object'=>array('box','ball'),
                 'color'=>array('blue','white'),
                 'texture'=>'hard',
                 'heavy'=>'no',
                 'name'
             );

我想做的是通過$blueprint運行$data並確保以下內容:

  1. 如果$data鍵/值對與$blueprint鍵/值對匹配,則$data data'sk / v有效
  2. 如果$data鍵/值對與$blueprint鍵和嵌套數組中的值匹配,則$data data'sk / v有效
  3. 如果$data數組省略了$blueprint中存在的鍵/值對,則$data data'sk / v如果不在$required數組中,則可能仍然有效。
  4. 如果$data陣列提供一個密鑰/值對不存在於$blueprint$data “SK / v是無效
  5. 如果鍵/值對中的$data鍵與沒有定義鍵的$blueprint值匹配,則$data data'sk / v仍然有效。 但是,如果$blueprint同時定義了鍵和值,則$data data'sk / v必須滿足規則1的要求才有效。
  6. 我想對$blueprint k / v施加字符限制,如果$data data'sk / v超出此字符限制,則$data sk / v無效

如果$data data'sk / v無效,那么我想以某種方式將錯誤與該特定k / v相關聯,以描述其無效的原因(超出字符限制,常規錯誤等)。也許該錯誤將在第三陣?

我已經研究了array_intersect_assoc但是不確定這是否超出了該函數的范圍。 另外, $blueprint中將有大量的值,因此我需要盡可能多用途的東西。

我認為這是對的,在撰寫本文時,我的大腦有些融化,所以請不要猶豫,請問是否感到困惑。 我最好單獨驗證每個k / v嗎?

讓我們看看誰是最聰明的人。

我對您的示例代碼進行了更改。 如果將名稱設置為鍵而不是數字鍵值,則似乎更容易。

$required = array(
 'color'=>array('white','blue'),
 'heavy'=> 'no',
 'name' => '', # name now a key
);

現在,這適用於您的許多規則。 首先檢查必需的鍵是否存在,並且除了必需和藍圖外,不存在其他多余的鍵。

# check required keys
$missing = array_diff_key($required, $data);
if($missing) {
  var_dump($missing); # react to missing keys
}

# check against all possible keys
$possible = array_merge_recursive($blueprint, $required);
$extra = array_diff_key($data, $possible);
if($extra) {
  var_dump($extra); # react to extra keys
}

現在,剩下的我真的需要知道您如何處理格式錯誤的數據等。但是,如果您的數據現在通過了這兩項測試,並且您以自己認為合適的方式進行響應,則應該清楚地遍歷數組並使用array_search()驗證array_search()filter_var()來檢查長度。

說實話,這本身並不困難,只是復雜。 您可以使用array_map函數簡化映射。 它看起來像這樣:

function validate_data($data, $blueprint)
{
    // an implementation of all that stuff you wrote using lots of
    // for loops and if statements
}

array_map('validate_data', $data, $blueprint);

查看手冊頁以獲取更多詳細信息。 這次你可以成為傻子:)

您要使用in_array()。 它將搜索數組的值並找到不同的值,例如。

foreach($data as $key => $val) {
  $check = in_array($val, $blueprint);
  if($check === false) {
    print("invalid");
    die;
  }
}

我覺得很傻,但這是一種蠻力方法。 #6您免費獲得,因為它絕不在任何意義上。

foreach ($data as $k => $v) {
    if (empty($blueprint[$k])) {
        // (3) Data defines a key that isn't defined in blueprint.
    } else {
        if (is_array($blueprint[$k])) {
            if (in_array($v, $blueprint[$k])) {
                // (2) Data defines a valid value in a blueprint list.
            } else {
                // (also 4) Data defines a value not in a blueprint list.
            }
        } else if ($v == $blueprint[$k]) {
            // (1) Data defines a value in the blueprint.
        } else if (in_array($v, $blueprint)) {
            // (5) Data is in the blueprint without a key.
        } else {
            // (4) Data is invalid.
        }
    }
}

編輯:這是檢查$ blueprint是否具有$ data未定義鍵的循環。 在運行它之前,可能應該有一個切換開關,以確保完全有必要(在上一個塊中)。

foreach ($blueprint as $k => $v) {
    if (empty($data[$k])) {
        // (6) Data doesn't have a required key from blueprint.
    }
}

是的,您可能必須自己編寫代碼,因為我認為沒有任何內部函數可以做到這一點。 不應太難,因為您已經對需求有很好的描述-只需將其翻譯成PHP。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM