0

I am writing a utility to do some data loading from text files. I have a collection of dictionary files that i am processing into arrays that define the structure of the data files. Yes, i could use classes or otherwise, but using arrays, i have been stumped.

Let's say i have 3 files that i read and load into arrays. When i read the arrays, the 3rd instance has elements of the second despite trying to use unset and other things. What am I missing? I am on cygwin using php 5.3.16

Following are examples of list, but not the real lists. so, please disregard the substr statements as they are not real

fname c 1 16
lname c 17 30

addr c 1 20
city c 21 30
state c 31 40
zip n 41 45

bday d 1 9
ssn c 10 18

when loading with the code below, the 3rd array has elements from the second, namely bday, ssn, state and zip.

$cnt = 0;
while ($s = fgets($fp, 1024)) {
    $fldprops = array();
    $fldprops[0] = trim(substr($s,0,8));
    $fldprops[1] = trim(substr($s,9,1));
    $fldprops[2] = trim(substr($s,11,3));
    $fldprops[3] = trim(substr($s,15,3));
    $flds[$cnt] = $fldprops;
    $cnt++;
    unset($fldprops);
}

I had thought either one of $fldprops = array(); or unset() would clear the array but it is not working.

Update: I mistook the point of failure. it is not apparently at writing to the outer array, but in reading. As i mentioned in a comment, later in the code, i have a foreach loop and here it is failing:

foreach ($flds as $fldprop) {
     var_dump($fldprop);
}

here, i get bday, ssn, state and zip (the last two entries of the second array merged with the 3rd array).

5
  • 1
    Can you please post one value of $s? Commented Oct 5, 2012 at 15:15
  • $fldprops = array() creates a new empty array, so how do you determine that it's not working? Commented Oct 5, 2012 at 15:19
  • Can you please rephrase the question. I have a hard time understanding what you are trying to do and where it goes wrong. Commented Oct 5, 2012 at 15:19
  • @user1723406 how do you want the output to look like ? Commented Oct 5, 2012 at 15:19
  • It's because you forgot the vowels in the word field. I'm sure that's it. Commented Oct 5, 2012 at 15:21

2 Answers 2

1

This is the output I am getting:

<?php
    $flds = array();
    $cnt = 0;
    $s = "HelloWorldNiceToSeeYou";
    $fldprops = array();
    $fldprops[0] = trim(substr($s,0,8));
    $fldprops[1] = trim(substr($s,9,1));
    $fldprops[2] = trim(substr($s,11,3));
    $fldprops[3] = trim(substr($s,15,3));
    $flds[$cnt] = $fldprops;
    $cnt++;
    unset($fldprops);
    var_dump($flds, $cnt, $fldprops);
?>

Output:

<br />
<b>Notice</b>:  Undefined variable: fldprops in <b>/code/MQpnac</b> on line <b>13</b><br />
array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(8) "HelloWor"
    [1]=>
    string(1) "d"
    [2]=>
    string(3) "ice"
    [3]=>
    string(3) "oSe"
  }
}
int(1)
NULL

PHP Notice:  Undefined variable: fldprops in /code/MQpnac on line 13

So PHP is doing it right. Check out live at: http://codepad.viper-7.com/MQpnac

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

3 Comments

Thank you - turns out i wasn't testing the failure in the right place. In creating the array and assigning it to the outer array, the values are fine. the issue, as @Jack points out is in the use. So, later in the code, i have this: foreach($arr as $arrelem) and when i hit the 3rd one, i am getting old references to the previous entry.
Praveen, do you have any insight into why in my using a foreach loop, i am somehow getting references to elements in earlier arrays. See my update above - i did a var_dump on the creation side and things seem find, but then a var_dump in the midst of the foreach loop and in the 3rd iteration, i see properties from the 2nd nested array. thanks.
Nevermind - stupid inner/outer loop issue... thanks for your help.
0

Why even bother with the temporary variable?

$cnt = 0;
while ($s = fgets($fp, 1024)) {
    $flds[$cnt] = array(
        trim(substr($s,0,8)), 
        trim(substr($s,9,1)), 
        trim(substr($s,11,3)), 
        trim(substr($s,15,3)), 
    )
    $cnt++;
}

2 Comments

This is helpful, but not a solution to my problem. Thanks.
No worries, I must have misunderstood the problem. I still don't think I understand what the problem was, but it sounds as though Praveen Kumar has sorted it anyway for you. So you should click on the tick next to his answer to mark it as accepted.

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.