php - "Unsanitizing"数组中的元素

标签 php arrays

我正在做什么:我有一个 txt 文件,其中包含项目#。我还有一个图像目录,其中这些项目 # 作为其文件名的前缀,但后面还有一些其他字符串(由 - 分隔),然后是文件扩展名 (.jpg)。

问题:我的函数运行良好,但结果数组($overlap)需要采用图像最初的格式,因为我将此数组写入 CSV 文件,以便我可以导入它。但为了找到匹配项,我必须删除图像文件名的原始格式(因此 array_intersection() 可以工作),但现在我需要恢复它。

这是当前代码:

function test() {

    function sanitize_items($value) {
        # Convert to uppercase, remove whitespace, then remove .JPG extension.
        # If the name has a "-", return the part before it.
        $base = basename( trim(strtoupper($value)), '.JPG' );
        $temp = explode('-', $base);
        return $temp[0];
    }

    $file = file('items.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $img = scandir('img');

    $file = array_map('sanitize_items', $file);
    $img  = array_map('sanitize_items', $img);

    $overlap = array_intersect($file, $img);
    echo "<pre>";
    print_r($overlap); # returns the matches, but not in the format I need.
}

这是$overlap当前返回的内容:

Array
(
[0] => DEC308A
[1] => DEC308B
[2] => DEC309A
...
)

我需要它返回交集,但它是运行 sanitize_items() 之前的方式:

Array
(
[0] => DEC308A-2020.jpg
[1] => DEC308B-2020.jpg
[2] => DEC309A-2300.jpg
...
)

最佳答案

我建议不要使用array_map()(它无法保留数组键)来应用清理函数,而是建议在两个数组上执行foreach()循环输入以生成一个新数组,该数组将 DEC308A 等字符串作为关联数组键,并将原始完整 .jpg 字符串作为其值。然后您可以调用array_intersect_key()跨越它们以产生交集输出,其中包含缩短/清理的字符串(在键中)和原始字符串(在值中)。

// Keep this function as it is...
function sanitize_items($value) {
    # Convert to uppercase, remove whitespace, then remove .JPG extension.
    # If the name has a "-", return the part before it.
    $base = basename( trim(strtoupper($value)), '.JPG' );
    $temp = explode('-', $base);
    return $temp[0];
}

$file = file('items.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$img = scandir('img');

// Temporary sanitized arrays:
$file_sanitized = array();
$img_sanitized = array();

// Loop over the input arrays and store them as key => value
// pairs, where the sanitized string is the key
foreach ($file as $f) {
  // Produces $file_sanitized['DEC308A'] = 'DEC308A-2020.jpg';
  $file_sanitized[sanitize_items($f)] = $f;
}
// Same kind of thing for $img...
foreach ($img as $i) {
  $img_sanitized[sanitize_items($i)] = $i;
}

// Now compare for intersection by key
$overlap = array_intersect_key($file_sanitized, $img_sanitized);

//-------------------------------- 
// Note, not sure of your inputs but you might need to reverse the args favoring
// $img_sanitized so you get those resultant values rather than what was in the file...
// $overlap = array_intersect_key($img_sanitized, $file_sanitized);

echo "<pre>";
print_r($overlap);

如果您确实想要在末尾使用纯数字键而不是像 DEC308A 这样的字符串,只需调用 array_values() 即可:

$overlap = array_values($overlap);

关于php - "Unsanitizing"数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126930/

相关文章:

php - 用于查找具有约束的三个表之间的关系的 MySQL 查询,即使该关系不存在

php - 来自文件的反射类 PHP?

java - 在android上解密php加密数据

javascript - 用 'object variable' 指向 'string value' 并动态更新它?

c - C代码如何运行?

javascript - 在javascript的帮助下从字符串中获取数字

php - 如何延迟 Ajax 请求?

php - 如何使用字符串值查询 Google Spreadsheets API?

php - 如何使单选按钮返回 bool 值真/假而不是开/关

arrays - Angular - umd.js 与 rx.umd.js