Cristina
Cristina

Reputation: 77

How can I create an array using pair of values in PHP?

I have a list of status and subtsatuses that I need to "translate"/convert to a new one, just like in the example below:

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

Is there a way I can declare an array in pairs (via multidimension array), or combined with classes, in order to make this conversion?

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

Upvotes: 0

Views: 68

Answers (1)

Adam
Adam

Reputation: 359

Assuming you have this as a text file and can read each line in turn I would probably do the following. I noticed that your values are separated by 2 or more spaces. This is a good way of identifying the delimeter.

So this example is working on just one of those lines but assume you are looping through each line and doing this.

// 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>";

Hope that helps, or sparks some ideas!! PS - I realise that the large spaces could be single tabs (\t) but I'm going to guess they are just spaces for the sakes of the example :)

Upvotes: 0

Related Questions