You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP处理CSV多维数组:将指定列拆分为三列并扩展数组

Got it, let's work through this problem together. You've got a multi-dimensional product array where each sub-array holds 18 columns of product details, and you need to split one specific column into three new columns (expanding each entry to 21 total columns) only when it matches a target string. If the preg_match fails, you want columns 6,7,8 to stay empty—here's how to pull this off cleanly, using PHP since you referenced preg_match:

Solution for Splitting a Product Array Column Conditionally

Step 1: Nail down your target column and regex pattern

First, confirm which column you need to check (remember arrays are 0-indexed in most languages like PHP, so adjust the index accordingly). Then craft a regex pattern that captures the three distinct parts you want to split into columns 6,7,8. For example, if your target string looks like Type-001-Status, your regex would capture each hyphen-separated segment.

Step 2: Loop through each product and process the data

Iterate over every sub-array in your product list. For each entry:

  • Grab the value from your target column
  • Run preg_match to test against your pattern
  • If the match succeeds: populate columns 6,7,8 with the captured segments
  • If it fails: leave those three columns empty
  • Ensure every sub-array ends up with exactly 21 columns

Example Code Implementation

// Sample original product array (18 columns per entry)
$productList = [
    ["Laptop", "Electronics", "SKU123", "...", "OriginalString1", "...", "Col18"],
    ["Mug", "HomeGoods", "SKU456", "...", "NonMatchingString", "...", "Col18"],
    // Add more products here
];

// Define your target column (0-indexed; adjust to your actual column)
$targetCol = 4;
// Regex pattern to capture 3 groups (tweak this to match your string format)
$splitPattern = '/^(\w+)-(\d{3})-(\w+)$/';

// Process each product entry
foreach ($productList as &$product) {
    // Initialize columns 6,7,8 (0-indexed = 5,6,7) as empty by default
    $product[5] = "";
    $product[6] = "";
    $product[7] = "";

    $targetValue = $product[$targetCol];
    if (preg_match($splitPattern, $targetValue, $matches)) {
        // Populate the three columns with captured regex groups
        $product[5] = $matches[1];
        $product[6] = $matches[2];
        $product[7] = $matches[3];
    }

    // Ensure the array has exactly 21 columns (add empty values if needed)
    while (count($product) < 21) {
        $product[] = "";
    }
}
unset($product); // Clear the reference to avoid accidental edits later

Key Things to Adjust

  • Regex Pattern: Modify $splitPattern to match your specific string structure. If your target uses different separators or formats, update the regex accordingly (e.g., use \/ instead of - if slashes are your delimiter).
  • Column Indices: Double-check if you're using 1-indexed or 0-indexed columns. The example uses 0-indexed, so columns 6,7,8 from your question map to indices 5,6,7 in the code.
  • Edge Cases: If your original array has entries shorter than 18 columns, the while loop ensures they get padded to 21 with empty values to keep consistency.

This approach guarantees every product entry is processed correctly—either with the split data in the specified columns, or empty values if the match doesn't land.

内容的提问来源于stack exchange,提问作者morwi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:05:22