开发者

help with preg_match_all and creating default values

I'm in need for php help/expertise to tweak/change this preg_match_all function. What I want to do is create default values for missing ordered pairs. It is matching the data correctly, but I need some logic to add some default values. (see EXPECTING data output below). Can this be handled within this same function? Thanks!

preg_match_all snippet:

foreach ($InputFile as $line){
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);
    $LineData = array();
    foreach ($matches as $information)
        $LineData[$information[2]] = $information[3];
    $data[] = $LineData;
print_r($LineData);
}

The data file has a max of (8) attributes in this order:

1-Server
2-Logdate
3-BackupSet
4-StartTime
5-Duration
6-DBServer
7-Size
8-Status

But there are instances in the file, where there are lines that do NOT have all the attributes, such as 5-Duration, 7-Size, 8-Status. I want to place a default value for these attributes something like:

5-Duration => 0
7-Size     => 0
8-Status   => incomplete

Array output: (current)

Array ( [Server] => hostname3.prop.abc
        [Logdate] => Wed Jan 05 2011
        [BackupSet] => rfoo101.az1
        [StartTime] => 20110105000004
        [Duration] => 00:56:47
        [DBServer] => rfoo101.prop.az1.kaz.com 
        [Size] => 56.51
        [Status] => Backup succeeded )
Array ( [Server] => hostname3.prop.abc
        [Logdate] => Wed Jan 05 2011
        [BackupSet] => bar202.az4_lvm
        [StartTime] => 20110105040003
        [DBServer] => bar202.prop.az4.kaz.com) 
Array ( [Server] => hostname10.prop.az2
        [Logdate] => Thu Jan 06 2011
        [BackupSet] => bar201_az2_lvm
        [StartTime] => 20110106151622
        [D开发者_JS百科BServer] => bar201.prop.az2.kaz.com  
        [Status] => Backup failed )

Expecting data output: (WITH DEFAULT VALUES)

Array ( [Server] => hostname3.prop.abc
        [Logdate] => Wed Jan 05 2011
        [BackupSet] => rfoo101.az1
        [StartTime] => 20110105000004
        [Duration] => 00:56:47
        [DBServer] => rfoo101.prop.az1.kaz.com 
        [Size] => 56.51
        [Status] => Backup succeeded )
Array ( [Server] => hostname3.prop.abc
        [Logdate] => Wed Jan 05 2011
        [BackupSet] => bar202.az4_lvm
        [StartTime] => 20110105040003
        [Duration] => 0 
        [DBServer] => bar202.prop.az4.kaz.com 
        [Size]     => 0  
        [Status]   => incomplete) 
Array ( [Server] => hostname10.prop.az2
        [Logdate] => Thu Jan 06 2011
        [BackupSet] => bar201_az2_lvm
        [StartTime] => 20110106151622
        [Duration]  => 0
        [DBServer] => bar201.prop.az2.kaz.com 
        [Size]   => 0 
        [Status] => Backup failed )

Code changes per help:

<?php
$defaults = array(
    'Duration' => 0,
    'Size'     => 0,
    'Status'   => 'incomplete' );

$data = array();
$InputFile = file("test.txt");

foreach ($InputFile as $line){
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER); 
    $LineData = array();
    foreach ($matches as $information)                  
        $LineData[$information[2]] = $information[3];  
    $data[] = array_merge($defaults, $LineData);
}

print_r($data);
?>


You can make an array of default values and then use array_merge() to create an output array that has the default values except where overridden by values from the parsed match:

$defaults = array(
    'Duration' => 0,
    'Size' => 0,
    'Status' => 'incomplete' );

$output_with_defaults = array_merge($defaults, $output_without_defaults);

This works due to the following behavior from array_merge():

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

For your case, you'd want to modify this line:

$data[] = $LineData;

to be...

$data[] = array_merge($defaults, $LineData);

(And declare the defaults array before the start of any of the current code - it doesn't need to be inside any loops.)


You probably can assign a default array structure, like

if (!is_array($LineData))
{
  $LineData = 
  array(
    'Server'    => ...,
    'Logdate'   => ...,
    'BackupSet' => ...,
    'StartTime' => ...,
    'Duration'  => 0,
    'DBServer'  => ...,
    'Size'      => 0,
    'Status'    => 'incomplete',
  );
}
foreach ($matches as $information)
{
  $LineData[$information[2]] = $information[3];
}
$data[] = $LineData;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜