如何在PHP中使用值对创建数组?

时间:2014-03-10 15:15:31

标签: php arrays class multidimensional-array

我有一个状态和子句子列表,我需要“翻译”/转换为新的,如下例所示:

Old Status        Old Substatus     New Status          New Substatus
-----------------------------------------------------------------------
4-Defer          Code Freeze           11          Code/Hardware Bug (Response/Resolution), Dev Priority = 4, Confirmed = ‘Y’
4-Defer          Future Project        11          Code/Hardware Bug (Response/Resolution), Dev Priority = 4, Confirmed = ‘Y’
4-Defer          No Plan to Fix        11          Code/Hardware Bug (Response/Resolution), Dev Priority = 4, Confirmed = ‘Y’
4-Defer      No Resource Available     11          Code/Hardware Bug (Response/Resolution), Dev Priority = 4, Confirmed = ‘Y’
...
11-Closed           Duplicate          96          Closed, Duplicate Bug
11-Closed        Not Reproducible      91          Closed, Could Not Reproduce
11-Closed         Not a Defect         92          Closed, Not a Bug

有没有办法可以成对地声明一个数组(通过多维数组),或者与类结合,以便进行这种转换?

something like    (11, "Duplicate") => (96, "Closed, Duplicate Bug")

非常感谢任何帮助。

提前致谢!

1 个答案:

答案 0 :(得分:0)

假设您将此作为文本文件并且可以依次读取每一行,我可能会执行以下操作。我注意到你的值被2个或更多个空格分隔。这是识别分界仪的好方法。

所以这个例子只是处理其中一行,但假设你循环遍历每一行并执行此操作。

// Here's just one line, pushed into $str
$str    = "4-Defer          Code Freeze   11    Code/Hardware Bug etc";

// Use the multiple spaces as a delimeter and replace them with #^# (a single delimeter)
$str    = preg_replace("/[ ]{2,99}/i","#^#",$str);

// Explode on the delimeter
$str    = explode("#^#",$str);

// Now you have an array containing each column data
// You can build your new array by directly referring to the columns
print "<pre>";
print_r($str);
print "</pre>";

希望有所帮助,或激发一些想法! PS - 我意识到大空间可以是单个标签(\ t)但是我猜它们只是示例中的空间:)