2

我有一个 .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 很陌生。

4

5 回答 5

2

只需一点点数组迭代就可以解决问题:explode, array_map, trim,issetforeach.

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. =循环遍历每一行并再次使用分解每个部分explode
  3. 用于array_map循环每个值并删除空格
  4. 检查以确保我们有 3 个带有 的值isset,如果没有,则跳过迭代
  5. 将我们收集的值推入输出数组
于 2013-08-13T20:16:31.607 回答
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. 爆炸
  2. 数组映射
  3. 修剪
  4. 数数
  5. 下层
  6. 伊塞特
  7. array_push
  8. array_unique
于 2013-08-13T20:12:05.210 回答
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
        )

)
*/
?>
于 2013-08-13T20:16:10.973 回答
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);
于 2013-08-13T20:16:49.573 回答
-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:"
  }
}
于 2013-08-13T20:09:30.397 回答