0

I have .txt file formatted as such:

    "id","dealer_id","vin","stockno",
    "1","2","3","4",
    "5","6","7","8",
    "9","10","11",12"

My goal is push this into an associative array like such:

    "id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"

My question is, the data loops through for every 4 entries. In the example above, I should have 3 arrays created like such:

    "id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
    "id"=>"5", "dealer_id"=>"6", "vin"=>"7", "stockno"=>"8"
    "id"=>"9", "dealer_id"=>"10", "vin"=>"11", "stockno"=>"12"

How can I make this happen within PHP -- if possible at all?

0

4 Answers 4

4

Here's one approach:

<?php
$file = fopen('data.txt', 'r');
$headers = fgetcsv($file);
$result = array();
while ($row = fgetcsv($file)) {
        if (!$row[0]) continue;
        $nextItem = array();
        for ($i = 0; $i < 4; ++$i) {
                $nextItem[$headers[$i]] = $row[$i];
        }
        $result[] = $nextItem;
}
fclose($file);
var_dump($result);

This uses fgetcsv to read each delimited row of the file. The first row is used as the header names. Then, for each other row, we create a new entry and match the indexes to the header names. I set it to 4 because you have a trailing delimiter that creates an empty entry at the end, if you removed those from the input you could use the length of $headers for the inner loop.

Here is the result of the var_dump for your data:

array(3) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    ["dealer_id"]=>
    string(1) "2"
    ["vin"]=>
    string(1) "3"
    ["stockno"]=>
    string(1) "4"
  }
  [1]=>
  array(4) {
    ["id"]=>
    string(1) "5"
    ["dealer_id"]=>
    string(1) "6"
    ["vin"]=>
    string(1) "7"
    ["stockno"]=>
    string(1) "8"
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(1) "9"
    ["dealer_id"]=>
    string(2) "10"
    ["vin"]=>
    string(2) "11"
    ["stockno"]=>
    string(3) "12""
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Sure, that's possible.

use http://www.php.net and read about the following functions

  • file_get_contents()
  • explode()
  • str_replace()

I'll provide a short example:

$file = file_get_contents('myfile.txt');
$parts = str_replace('"', '', explode(',',$file);

And then do a foreach-loop or something to add the key-value pairs in your array.

Good luck

Comments

0
$getFileContents    = file_get_contents("demo.txt");
$explodeBr          = explode("\n", str_replace('"',"",$getFileContents));
$assocKeyStr        = array_filter(explode(",", trim($explodeBr[0])));
$otherDetails       = array_slice($explodeBr, 1, count($explodeBr));
$resultingArr       = array();
foreach($otherDetails as $indDetailStr) {
    $expDetail      = array_filter(explode(",", trim($indDetailStr)));
    $resultingArr[] = array_combine($assocKeyStr, $expDetail);
}
print_r($resultingArr);

Terms used:

  1. file_get_contents which reads the file, within the server.

  2. explode forms an array by dividing the given input needle, here in your case \n and ,

  3. array_filter to callback, if no callback passed, by default this returns not null associative values.

  4. array_slice to create another array with limited and required number of arrays.

  5. count to get the length of array.

  6. array_combine expects two parameters with equal length arrays, first parameter array are the keys, second parameter to form values.

Comments

-1

Definitely it's possible - i.e. file returns you all lines from file, foreach will let you iterate through lines, then you can use trim, explode and array_combine to build desired structure.

1 Comment

would be nice if downvoter could explain why downvoting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.