JohanStaart
JohanStaart

Reputation: 75

Array Keys do not add up but makes keys go from 0 to 9 3 times

When I was combining 4 arrays into 1 array it made the keys [0] to [9] 4 times instead of making it go from [0] to [34].

The file I'm working with is a .txt file with the follwing content (just 4 lines with random text):

gghondxs
vishfgfh
magiedhv
bvmdomvb

<form method="post" action="index.php" enctype="multipart/form-data">
    <input class="file-choise" name="file" type="file" />
    <input class="upload-button" type="submit" value="Upload Bestand" />       
</form>

    $file = fopen($_FILES['file']['tmp_name'], "rb");
    $linearray = array();
    $workingarray = array();
    while(! feof($file)) {
        $line = fgets($file);
        echo "$line<br>";
        $linearray[] = (str_split("$line"));
    }

    $workingarray[] = array($linearray,"endline");
    print_r($workingarray);

The output that I gets is the following;

Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => g [1] => g [2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => [9] => ) [1] => Array ( [0] => v [1] => i [2] => s [3] => h [4] => f [5] => g [6] => f [7] => h [8] => [9] => ) [2] => Array ( [0] => m [1] => a [2] => g [3] => i [4] => e [5] => d [6] => h [7] => v [8] => [9] => ) [3] => Array ( [0] => b [1] => v [2] => m [3] => d [4] => o [5] => m [6] => v [7] => b [8] => [9] => ) [4] => Array ( [0] => ) ) [1] => endline ) )

I want it to look like this

Array ( [0] => Array [0] => g [1] => g [2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => v [9] => i [10] => s [11] => h [12] => f [13] => g [14] => f [15] => h [16] => m [17] => a [18] => g [19] => i [20] => e [21] => d [22] => h [23] => v [24] => [25] => b [26] => v [27] => m [28] => d [29] => o [30] => m [31] => v [32] => b [33] => [34] => endline ) )

Can someone help me achieve this?

Thanks

Upvotes: 0

Views: 64

Answers (4)

user2762134
user2762134

Reputation:

<?php
$file = fopen($_FILES['file']['tmp_name'], "rb");

$linearray = array();
$workingarray = array();

while(! feof($file)) {
    $line = fgets($file);

    echo "$line<br>";

    $line = str_split(trim($line));

    $linearray = array_merge($linearray, $line);
    // $linearray = array_merge($linearray, $line, array("endline")); Include endline after each actual line.
}

$workingarray[] = $linearray;

var_dump($workingarray);

Upvotes: 1

Ravi Hirani
Ravi Hirani

Reputation: 6539

Just add one line in your existing code:-

$workingarray[] = array($linearray,"endline");
// This is a new line
$workingarray = array_values($workingarray);    
print_r($workingarray);

array_values() will regenerate key index in your array start with 0.

Upvotes: 0

Adam Taylor
Adam Taylor

Reputation: 7793

Can you use array merge?

$linearray[] = array_merge( $linearray, str_split("$line") );

http://php.net/manual/en/function.array-merge.php

Upvotes: 0

FZE
FZE

Reputation: 1627

$workingarray = call_user_func_array('array_merge', $workingarray);

Upvotes: 0

Related Questions