Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:23:17+00:00 2026-05-19T04:23:17+00:00

I’m in need for php help/expertise to tweak/change this preg_match_all function. What I want

  • 0

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
        [DBServer] => 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);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:23:17+00:00Added an answer on May 19, 2026 at 4:23 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.