将txt文件分解为多维数组

时间:2013-08-13 20:06:09

标签: php arrays text multidimensional-array explode

我有一个.txt文件,如下所示:

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

我想爆炸上面的文件来制作一个如下所示的数组:

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

我怎样才能做到这一点?我已经尝试过使用类似explode(":", $data)的东西,但我不知道如何使用它来做我想要的事情。我对PHP很新。

5 个答案:

答案 0 :(得分:2)

只需要进行一些数组迭代即可实现:explodearray_maptrimissetforeach

PHP示例

$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代码输出

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
        )

)

代码解释

  1. 使用explode(":", $txt)
  2. 分隔这些行
  3. 循环显示每一行,然后使用=
  4. 再次按explode拆分每个部分
  5. 使用array_map循环每个值并删除空格
  6. 检查以确保我们有isset的3个值,如果没有,则跳过迭代
  7. 将我们收集的值推送到输出数组

答案 1 :(得分:1)

这样的东西?

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

以下是我在代码中使用的功能列表,并链接到这些功能文档

  
      
  1. explode
  2.   
  3. array_map
  4.   
  5. trim
  6.   
  7. count
  8.   
  9. strtolower
  10.   
  11. isset
  12.   
  13. array_push
  14.   
  15. array_unique
  16.   

答案 2 :(得分:0)

尝试重复使用此代码:

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

)
*/
?>

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

你有这样的爆炸:

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

结果是:

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:"
  }
}