Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a .txt file that looks like this:

Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:
Test = 346192 = August 9, 2013:

Then, I use the following PHP code...

$Output = array();
$Lines = explode(":", $txt);

foreach($Lines as $line) {
    $Rows = array_map('trim', explode(" = ", $line));
    if(!isset($Rows[0], $Rows[1], $Rows[2])) continue;
    $Output[$Rows[0]] = array($Rows[1], $Rows[2]);
}

print_r($Output);

...to turn the .txt file into a multidimensional array that looks like this:

Array
(
    [Test] => Array
        (
            [0] => 346192
            [1] => August 9, 2013
        )

    [56cake] => Array
        (
            [0] => 0
            [1] => August 6, 2013
        )

    [Wwe] => Array
        (
            [0] => 812986192
            [1] => August 6, 2013
        )
)

However, there is a BIG error. The code removes all of the duplicate data values. In my example txt file, I had TWO values with the name "Test" however the code only outputs ONE in the multidimensional array.

You can also notice how the code replaced the data of the first "Test" element (in the multidimensional array) with the latest one (last line in the .txt file).

The data for the first "Test" element in the array DOES NOT even match the data in the first line of the .txt file Test = 10849831 = August 6, 2013:.

How can I resolve this issue? I want the multidimensional array to look like this:

Array
(
    [Test] => Array
        (
            [0] => 10849831
            [1] => August 6, 2013
        )

    [56cake] => Array
        (
            [0] => 0
            [1] => August 6, 2013
        )

    [Wwe] => Array
        (
            [0] => 812986192
            [1] => August 6, 2013
        )

    [Test] => Array
        (
            [0] => 346192
            [1] => August 9, 2013
        )
)
share|improve this question
2  
You can't have the same key twice in an array. That's all. –  Tobias Kun Aug 13 '13 at 21:22
 
you cant have identical keys. Test in your case –  Dagon Aug 13 '13 at 21:22
 
What @TobiasKun said. If your keys aren't unique then you shouldn't be using them as keys. –  Sammitch Aug 13 '13 at 21:35
add comment

3 Answers

up vote 1 down vote accepted

you cant have identical keys. Test in your case. I suggest the alternative

foreach($Lines as $line) {
    $Rows = array_map('trim', explode(" = ", $line));
    if(!isset($Rows[0], $Rows[1], $Rows[2])) continue;
    $Output[] = array('type'=>$Rows[0],'id'=>$Rows[1],'date'=> $Rows[2]);
}

type,id,date; where my guess

share|improve this answer
 
+1 for providing working code :) –  Tobias Kun Aug 13 '13 at 21:26
add comment

You can't have the same key twice in an array. That's all.

So when you add the "new" entry, the old value is overwritten.

Example:

$data = array("test" => "Content 1");
$data["test"] = "Content 2";

echo "<pre>" . print_r($data,1) . "</pre>";
//Will give you:
[test] => "Content 2"
share|improve this answer
 
+1 for um, just being you! –  Dagon Aug 13 '13 at 21:27
 
@Dagon: Well thx :D –  Tobias Kun Aug 13 '13 at 21:28
add comment

As others have pointed out, your code doesn't give the desired result because an array can not contain identical keys, so your data is being overwritten. Dagon's answer stores all the duplicates, but loses the ability to do key look up using the first element of each line of the input file.

An alternative that preserves the ability to do key look up is:

foreach($Lines as $line) {
    $Rows = array_map('trim', explode(" = ", $line));
    if(!isset($Rows[0], $Rows[1], $Rows[2])) continue;
    if (array_key_exists($Rows[0], $Output))
        $Output[$Rows[0]][] = array($Rows[1], $Rows[2]);
    else
        $Output[$Rows[0]] = array(array($Rows[1], $Rows[2]));
}        

Instead of holding the data in each row directly, each key in $Output holds an array with one element for each line with that key in your text file. Using your example data, the resulting array will look like:

Array
(
    [Test] => Array
        (   
            [0] => Array
            (
                [0] => 10849831
                [1] => August 6, 2013
            )
            [1] => Array
            (
                [0] => 346192
                [1] => August 9, 2013
            )
        )

    [56cake] => Array
        (
            [0] => Array
            (
                [0] => 0
                [1] => August 6, 2013
            )
        )

    [Wwe] => Array
        (
            [0] => Array
            (
                [0] => 812986192
                [1] => August 6, 2013
            )
        )
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.