0

I have data that look like:

 COVUP.0.db2inst1.NODE0000.CATN0000.20110304144237.001
 DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001
 DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125123615.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125153555.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101126162500.001

I need to store it as a variable that is array of array. In ruby I'd write

databases = {"COVUP"    =>["20110304144237"], 
             "DBB"      =>["20100730102440", "20100809104005"],
             "DOMINIC1" =>["20101125123615","20101125153555","20101125153555"]  }

where DBB will have two elements linked to it as there are two lines with DBB string in it.

  1. DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001
  2. DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001

    • How can I define such array and
    • How can I walk through such array?
1
  • can you write an example of how do you want the array to look like? Commented Mar 9, 2011 at 1:48

3 Answers 3

2

Using explode method:

$data = 'COVUP.0.db2inst1.NODE0000.CATN0000.20110304144237.001
DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001
DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001
DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125123615.001
DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125153555.001
DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101126162500.001';

$lines = explode("\n",$data);
foreach($line as $value){
    $line_data = explode(".",$value);
    $array[$line_data[0]][] = $line_data[5];
}

var_dump($array);

OUTPUT:

array
  'COVUP' => 
    array
      0 => string '20110304144237' (length=14)
  'DBB' => 
    array
      0 => string '20100730102440' (length=14)
      1 => string '20100809104005' (length=14)
  'DOMINIC1' => 
    array
      0 => string '20101125123615' (length=14)
      1 => string '20101125153555' (length=14)
      2 => string '20101126162500' (length=14)

Note that using php native functions is usually faster than the almighty RegEx, and this code is based on the structure example given.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess I was after this one $array[$line_data[0]][] = $line_data[5]; thank you
Hey Paul, i tested the code myself, arrays do not have to be defined before using in PHP, maybe you are confused with some other language.
it definitely works for me as written above. I like the simplicity of the code.
1
$str = 'COVUP.0.db2inst1.NODE0000.CATN0000.20110304144237.001
 DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001
 DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125123615.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125153555.001
 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101126162500.001';

preg_match_all('/^(.*?)\..*\.(\d+?)\.001$/m', $str, $matches);

$array = array();

foreach($matches[0] as $index => $match) {
    $array[trim($matches[1][$index])][] = $matches[2][$index];
}

var_dump($array);

Output

array(3) {
  ["COVUP"]=>
  array(1) {
    [0]=>
    string(14) "20110304144237"
  }
  ["DBB"]=>
  array(2) {
    [0]=>
    string(14) "20100730102440"
    [1]=>
    string(14) "20100809104005"
  }
  ["DOMINIC1"]=>
  array(3) {
    [0]=>
    string(14) "20101125123615"
    [1]=>
    string(14) "20101125153555"
    [2]=>
    string(14) "20101126162500"
  }
}

1 Comment

You have leading whitespace in your array indexes.
1
$data = "COVUP.0.db2inst1.NODE0000.CATN0000.20110304144237.001\n" .
    "DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001\n" .
    "DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001\n" .
    "DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125123615.001\n" .
    "DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125153555.001\n"
    "DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101126162500.001";
$dataList = explode("\n", $data);

// create the result array
$result = array();

foreach ($dataList as $d) {
   $columns = explode(".", $d);
   $name = $columns[0];
   $value = $columns[5];

   // add the key name to the top-level array
   if (!in_array($name, $result)) $result[$name] = array();

   // add the value to the array
   $result[$name][] = $value;
}

1 Comment

is your last if block equal to $array[$line_data[0]][] = $line_data[5]; ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.