user2680030
user2680030

Reputation: 53

Explode txt file into multidimensional array

I have a .txt file that looks like the one below:

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

I want to explode the file above to make an array that looks like the one below:

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

How can I accomplish this? I've already tried using something like explode(":", $data) but I don't know how to use it to do what I want above. I'm fairly new with PHP.

Upvotes: 2

Views: 1237

Answers (5)

bystwn22
bystwn22

Reputation: 1794

Something like this?

<?php
  // Sample text data
  $text = 'Test = 10849831 = August 6, 2013:
Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:';

  $text = explode( PHP_EOL, $text ); // split by lines using linebreaks
  // $text = explode( ":", $text ); // you can also split by lines with ':'  
  $arr  = array(); // Our array which holding the output
  // print_r( $text );

  foreach( $text as $line ) { // loop through lines
    $line = array_map( "trim", explode( "=", $line ) ); // split by '=' sign and do 'trim'
    // print_r( $line );
    if ( count( $line ) === 3 ) {
      $key  = strtolower( $line[0] ); // make the key lowercase (case insensitive)
      $val1 = $line[1];
      // process the date and remove day from date
      $val2 = trim( $line[2], ":" ); // remove ':' from the end of date
      $val2 = array_map( "trim", explode( " ", $val2 ) ); // split by space sign and do a 'trim'
      $val2 = $val2[0]." ".$val2[2]; // join year and month parts

      if ( isset( $arr[$key] ) ) { // check if the key already exists
        // key exists,  push new values
        array_push( $arr[$key], $val1, $val2 );
        continue; // key exists so continue the loop
      }
      // key doesn't exists in array so add the values
      $arr[$key]  = array( $val1, $val2 );
    }
  }

  // there is an altwernate way to remove duplicate values using 'array_unique'
  // uncomment below code if you dont want duplicate values
  /*if ( !empty( $arr ) ) {
    $arr  = array_map( "array_unique", $arr ); // remove duplicate values from array using 'array_unique'
  }*/

  print_r( $arr );
?>

Here is the list of functions which i used in my code and link to those functions documentation

  1. explode
  2. array_map
  3. trim
  4. count
  5. strtolower
  6. isset
  7. array_push
  8. array_unique

Upvotes: 1

You have use explode like this:

<?php
//
//Test = 10849831 = August 6, 2013:
//56cake = 0 = August 6, 2013:
//Wwe = 812986192 = August 6, 2013:

function read_data_file( $file ) {
    $file_open = fopen( $file , 'r' );
    $file_data = fread( $file_open , filesize( $file ) );
    fclose( $file_open );

    return $file_data;
}

$result_read = read_data_file('/home/josecarlos/Desktop/test.txt');

$result_explode = explode("\n", $result_read );

$final_result = array();

foreach ($result_explode as $key => $cursor){
    $explode = explode("=",  $cursor );
    if (isset($explode[0]) && $explode[0] != ''){
        $final_result[trim($explode[0])]  =array (trim($explode[1]),trim($explode[2])) ;
    }
}

var_dump($final_result);

The result is:

array(3) {
  'Test' =>
  array(2) {
    [0] =>
    string(8) "10849831"
    [1] =>
    string(15) "August 6, 2013:"
  }
  '56cake' =>
  array(2) {
    [0] =>
    string(1) "0"
    [1] =>
    string(15) "August 6, 2013:"
  }
  'Wwe' =>
  array(2) {
    [0] =>
    string(9) "812986192"
    [1] =>
    string(15) "August 6, 2013:"
  }
}

Upvotes: -1

phpisuber01
phpisuber01

Reputation: 7715

Just a little bit of array iteration does the trick with: explode, array_map, trim, isset and foreach.

PHP Example

$txt = <<<DOC
Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:
DOC;

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

PHP Code Output

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
        )

)

The Code Explained

  1. Break up the lines with explode(":", $txt)
  2. Loop through each line and break up each section by = again using explode
  3. Use array_map to cycle each value and remove whitespace
  4. Check to ensure we have 3 values with isset, if not, then skip the iteration
  5. Push our collected values into the output array

Upvotes: 2

Expedito
Expedito

Reputation: 7805

$chunks = explode(':', $text);
$out = array();
foreach ($chunks as $key => $value){
    $parts = explode('=', $value);
    if (count($parts) == 3){
        $out[trim($parts[0])] = array(trim($parts[1]), trim($parts[2]));
    }
}
print_r($out);

Upvotes: 0

user1646111
user1646111

Reputation:

Try to re-use this code:

<?php
echo '<pre>';
$result = array();

$string = 'Test = 10849831 = August 6, 2013';
$temp1 = explode("=", $string, 2);
print_r($temp1);
/*
Array
(
    [0] => Test 
    [1] =>  10849831 = August 6, 2013
)
*/
$key = $temp1[0];

$result[$key] = explode("=", $temp1[1]);

print_r($result);
/*
Array
(
    [Test ] => Array
        (
            [0] =>  10849831 
            [1] =>  August 6, 2013
        )

)
*/
?>

Upvotes: 0

Related Questions