如何定义和遍历数组的数组

时间:2011-03-09 01:43:30

标签: php arrays multidimensional-array

我的数据看起来像:

 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

我需要将它存储为一个数组数组的变量。在红宝石中,我写了

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

其中DBB将链接两个元素,因为其中有两行DBB字符串。

  1. DBB .0.db2inst1.NODE0000.CATN0000。的 20100730102440 0.001
  2. DBB .0.db2inst1.NODE0000.CATN0000。的 20100809104005 0.001

    • 如何定义此类数组和
    • 我如何通过这样的阵列?

3 个答案:

答案 0 :(得分:2)

使用explode方法:

$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);

<强>输出:

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)

请注意,使用php本机函数的通常比全能的RegEx更快,而且此代码基于给定的结构示例。

答案 1 :(得分: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);

输出

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"
  }
}

答案 2 :(得分: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;
}