简体   繁体   中英

PHP code is removing duplicates from multidimensional array

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

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"

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM