Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在转换为json之前验证csv中的数据?_Php_Json_Csv - Fatal编程技术网

Php 如何在转换为json之前验证csv中的数据?

Php 如何在转换为json之前验证csv中的数据?,php,json,csv,Php,Json,Csv,我有一个如下的csv文件:- +------+------+------------------------+ | name | mark | url | +------+------+------------------------+ | ABCD | 5 | http://www.example.org | | BCD | -2 | http://www.example.com | | CD | 4 | htt://www.c.co

我有一个如下的csv文件:-

+------+------+------------------------+
| name | mark |        url             |
+------+------+------------------------+
| ABCD |    5 | http://www.example.org |
| BCD  |   -2 | http://www.example.com |
| CD   |    4 | htt://www.c.com        |
+------+------+------------------------+
它包含名称、标记和url的标题。我正在使用PHP将数据从csv转换为json。我想在将其转换为json之前添加验证,比如UTF-8中的名称,标记应该是一个正数,介于0到5之间,url应该是有效的。如果行通过了所有验证,那么它将被存储在errorless.json中,如果任何行有任何问题,那么将被存储在error.json中,并带有一条注释“有什么问题”。csv到json的PHP代码:-

$fh = fopen("names.csv", "r");

$csvdata = array();

while (($row = fgetcsv($fh, 0, ",")) !== FALSE) {
    $csvdata[] = $row;
}

$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($csvdata));
fclose($fp);

我想知道如何添加这些验证以转换数据。我对这些概念还不熟悉,无法想出一种方法来做到这一点。如果有人能帮助我,我将不胜感激。

以下是您将要做的事情:

// to make life easier on yourself create 
// a function that checks one row of data 
// to make sure it is valid
// if a row is found to be invalid, the
// function will add an `error` field to the 
// array explaining the validation error   
function validateRow(&$data) {
    if (!array_key_exists('mark', $data) && ((int)$data['mark']) >= 0 && ((int)$data['mark']) <= 5) {
        $data['error'] = "No 'mark' value found between 0 and 5";
        return false;
    }
    // do validation for $data['name']
    // do validation for $data['url']
    return true;
}

$fh = fopen("names.csv", "r");

$csvdata = array();

while (($row = fgetcsv($fh, 0, ",")) !== FALSE) {
    $csvdata[] = $row;
}
$header = $csvdata[0];
$n = count($header);
$errorless = array();
$haserrors = array();
// here is where we convert the CSV data into
// an associative array / map of key value
// pairs by treating each row and the header 
// row as parallel arrays.  Start with index 1
// in the for loop to skip over the header row
// in csvdata
for ($row = 1; $row < count($csvdata); ++$row) {
    $data = array();
    for ($x = 0; $x < $n; ++$x) {
        $data[$header[$x]] = $csvdata[$row][$x];
    }
    // if we encounter no errors, 
    // add data to the errorless array
    if (validateRow($data)) {
        $errorless[] = $data;
    }
    else {
        $haserrors[] = $data;
    }
}

// you will want to do a parallel write
// for the error.json file
$fp = fopen('errorless.json', 'w');
fwrite($fp, json_encode($errorless));
fclose($fp);
//为了让自己的生活更轻松,创建
//检查一行数据的函数
//确保它是有效的
//如果发现一行无效,则
//函数将向
//解释验证错误的数组
函数validateRow(&$data){

如果(!array_key_exists('mark',$data)&((int)$data['mark'])>=0&((int)$data['mark'])您应该使用一种称为JSON模式的方法。您可以定义文档的格式。然后可以自动验证文档

如果您正在寻找CSV验证库的另一个具体实现,您应该首先查看packagist.org。

这可能会对您有所帮助

标准JSON格式不明确支持文件注释。它是用于存储和传输数据的轻量级格式。如果注释非常重要,您可以将其作为另一个数据字段(如注释)包含

输入

脚本


您可以使用json模式为数据定义一个模式,然后根据该模式验证数据。本文提供了有关您需要执行的操作的更多详细信息。@KartikeyVishwakarma:您尝试过吗?我运行脚本只是为了进行标记验证,但errorless.json还有第二行,该行有一个负值。忘记了转换字符串t奥因茨。
$ cat test.csv 
name,mark,url
ABCD,5,http://www.example.org
BCD,-2,http://www.example.com
CD,4,htt://www.c.com
<?php
function validate_url($url)
{
    return in_array(parse_url($url, PHP_URL_SCHEME),array('http','https')) && filter_var($url, FILTER_VALIDATE_URL);
}
function validate_mark($val)
{
    return ($val >= 0 && $val <= 5);
}

function errors($field)
{
    $errors = array(
        'url' => 'URL should be valid',
        'mark' => 'Mark should be between 0 to 5'
    );

    return ( isset($errors[$field]) ? $errors[$field] : "Unknown");
}

function csv2array_with_validation($filename, $delimiter = ",")
{
    $header = $row = $c_row = $output = $val_func =  array();
    if (($handle = fopen($filename, 'r')) !== FALSE) 
    {
        while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) 
        {
            if (empty($header))
            {
                $header = array_map('strtolower', $row);
                foreach ($header as $e) 
                {
                    $val_func[$e] = function_exists('validate_' . $e);
                }
                continue;
            }
            $c_row = array_combine($header, $row);
            $index = 'error_less'; $errors = array();
            foreach ($c_row as $e => $v) 
            {
                if ($val_func[$e]) 
                {
                    if (!call_user_func('validate_' . $e, $v)) 
                    {
                        $index = 'error';
                        $errors[$e] =  errors($e);
                    }
                }
            }
        /* 
          If the comment is truly important, 
          you can include it as another data field like errors,  
          comment below part if you do not wish to create new field (errors) in 
          json file 
        */
        if(!empty($errors))
        {
            $c_row['errors'] = $errors;
        }   
            $output[$index][] = $c_row;
        }
        fclose($handle);
    }
    return $output;
}

$output = csv2array_with_validation('test.csv');

// Write error.json
if (isset($output['error']) && !empty($output['error']))  
{
    file_put_contents('error.json', json_encode($output['error'], JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK));
}

// Write errorless.json
if (isset($output['error_less']) && !empty($output['error_less'])) 
{
    file_put_contents('error_less.json', json_encode($output['error_less'], JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK));
}
?>
$ php test.php
$ cat error.json 
[
    {
        "name": "BCD",
        "mark": -2,
        "url": "http:\/\/www.example.com",
        "errors": {
            "mark": "Mark should be between 0 to 5"
        }
    },
    {
        "name": "CD",
        "mark": 4,
        "url": "htt:\/\/www.c.com",
        "errors": {
            "url": "URL should be valid"
        }
    }
]
$ cat error_less.json 
[
    {
        "name": "ABCD",
        "mark": 5,
        "url": "http:\/\/www.example.org"
    }
]