SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Help on Array

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Help on Array

    Array Problem...

    Scenario:
    I've created a machine text data extractor which obtain a variable length array of data.
    The problem is getting the correct offset(varies from file to file)/key(label) to get the
    correct contentx(value).

    Example:
    file0000001 contains:
    array ( '0' =>
    array (
    label1,
    content1,
    '',
    label2,
    content2,
    label3,
    content3
    ),
    '1' =>
    array (
    label1,
    content1,
    '',
    label2,
    content2,
    label3,
    content3
    ),
    );


    Note:
    Array values varies from 176 to 280 elements and files ranges from 600 -> 57,000.
    Initial extracting functions are as follows,

    $fileExtracted = preg_split("/\s+/",$filexxxxxxx);
    $fileExtracted2 = array_filter($fileExtracted,"get_value"); //--- note get_value is a function to remove spaces
    $fileExtracted2 = array_values($fileExtracted2);

    $fileData_Ref = array_search("label3:",$prbExtracted2);
    $fileData_Ref += 1;

    $fileData = ($fileData_Ref > 0) ? $fileExtracted2[$fileData_Ref] : '';
    $fileData = (strlen($fileData) > 0) ? $fileData : '-'; //--- will get contentx else show '-'


    Problem occurs when contentx contains/is 0 or zero, offset returns wrong value giving labelx not contentx...maybe somebody could enlighten/suggest better solution, cheers.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im having a hard time following your problem and how it relates to your code.

    you are trying to parse a text file. what is the format of this text file? could you provide a sample?

    also, must the entire file be parsed into thier key/value pairs? or do you just need a single value from the file?

    please use [code] and [php] tags where appropriate.
    Last edited by clamcrusher; Oct 17, 2006 at 11:55.

  3. #3
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are the array[0], and array[1], containing the same values??? However it looks like there might be an easier way of parsing the file, than searching and calculationg the offset!

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically, i need to get contentx.., but how?

    Code:
    Example:
    file0000001 contains:
    array ( '0' =>
        array (
        label1,
        content1,
        '    ',
        label2,
       content2,
       label3,
       content3
       ),
       '1' =>
       array (
       label1,
       content1,
       '                        ',
       label2,
       content2,
       label3,
       '    ',
       content3
       ),
     );
    
    file0000002 contains:
    array ( '0' =>
        array (
        label1,
        '',
        content1,
        '    ',
        label2,
       content2,
       label3,
       content3
       ),
       '1' =>
       array (
       label1,
       content1,
       '      ',
       label2,
       content2,
       '',
       label3,
       '    ',
       content3
       ),
     );
    Codes so far...that doesn't give correct result...
    PHP Code:
    $fileExtracted preg_split("/\s+/",$filexxxxxxx);
    $fileExtracted2 array_filter($fileExtracted,"get_value"); //--- note get_value is a function to remove spaces
    $fileExtracted2 array_values($fileExtracted2);

    $fileData_Ref array_search("label3:",$prbExtracted2);
    $fileData_Ref += 1;

    $fileData = ($fileData_Ref 0) ? $fileExtracted2[$fileData_Ref] : '';
    $fileData = (strlen($fileData) > 0) ? $fileData '-'//--- will get contentx else show '-' 
    Note:
    Array values varies from 176 to 280 elements and files ranges from 600 -> 57,000.
    Initial extracting functions are as follows,

    Btw, spaces are of variable length so explode() is doesn't work either to split and assign each value in an array.

    Each text file is being generated by machine every hour and contains array of data like above.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Why are the array[0], and array[1], containing the same values??? However it looks like there might be an easier way of parsing the file, than searching and calculationg the offset!
    actually, it doesn't have the same value...my initial sample happens to only to have same array value.

    actually their an easier way to parse it...that's with your help.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your file actually contains php arrays? must you store them in this format?

    please use [code] tags so the forum displays your whitespace in your arrays. you can edit your post.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    your file actually contains php arrays? must you store them in this format?

    please use [code] tags so the forum displays your whitespace in your arrays. you can edit your post.

    nevermind my codes, how would you parse a file and get every contentx on each file containing variable length arrays...

    thanks for the help...btw.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so you have a php array stored in a file. did you post the real format? you need to, otherwise this is going to be very difficult to help you.


    you REALLY have unquoted elements in your array?
    Code:
    array ( '0' =>
        array (
        label1,
        content1,
        '    ',
        label2,
       content2,
       label3,
       content3
       ),

    normally it would be
    Code:
    array ( '0' =>
        array (
        'label1',
        'content1',
        '    ',
        'label2',
       'content2',
       'label3',
       'content3'
       ),
    is it possible these values themselves have quotes in them?
    Code:
    array ( '0' =>
        array (
        'label1',
        'content1 i have \'quotes\' in me',
    
       ),

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Btw, above is the actual file to be parsed...I was able to get an array and get the specific data I wanted like, site_name, operator, passed, test_time, device, etc...

    My problem is, each array of data generated is of variable length...


    *** update: problem solved...using text and preg functions.
    Last edited by l1nux_r00t; Oct 17, 2006 at 19:10.

  10. #10
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What output you want to generate from the above file?

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by REMIYA
    What output you want to generate from the above file?
    thanks remiya, it's solved already...using text and pregs_xxx functions.

  12. #12
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •