如何在php中创建具有爆炸文本的数组

时间:2012-09-20 17:39:12

标签: php arrays explode

毕竟我很抱歉我的英语。

我正在用 PHP 编写脚本,我遇到了一个小问题。

我们的想法是为每行文本创建一个数组,并用表示状态的字符串替换第四个值。

我有一行文字,例如 2022,33422,0,1,0,22

使用单行可以创建数组,但是使用多行会产生意外结果。

示例:

2022,33422,0,1,0,22

2024,3232,01,1,04,298762

        $myArray = explode(',', $uploadServer);
        $status = array(0  => "Unprocessed",
                 1  => "Processing",
                 2  => "Download aborted because the file became too big.",
                 3  => "Download aborted because the file downloaded too long.",
                 4  => "Download finished. Uploading to RapidShare.",
                 5  => "Upload to RapidShare finished. Job finished.",
                 7  => "Upload failed 3 times for unknown reasons. Job aborted.",
                 8  => "Upload failed because the file is black listed.",
                 9  => "Download failed for unknown reasons.",
                 11 => "Enqueued for later processing because this account already downloads 5 files at the same time.");



        foreach ($myArray as $valor) {
              if(array_key_exists($valor[3],$status)) {

                    return $passer[] = $status[$valor[3]];

              } else {

                    return FALSE;

              }
        }

$ myArray 的结果是

Array
(
    [0] => 2022
    [1] => 33422
    [2] => 0
    [3] => 1
    [4] => 0
    [5] => 22
)

但我需要这个最终数组

Array
(
[0]=>array(
          [0] => 2022
          [1] => 33422
          [2] => 0
          [3] => Processing
          [4] => 0
          [5] => 22
          )

[1]=>array(
           [0] => 2022
           [1] => 33422
           [2] => 0
           [3] => Processing
           [4] => 0
           [5] => 22
          )
)

有什么想法吗? 感谢

2 个答案:

答案 0 :(得分:0)

如果我理解正确的话,你就是在思考解决方案。

   foreach ($myArray as $valor) { 
          if(isset($status[$valor[3]])) {  # this checks if there is a status with that index/key
                 return $passer[] = $status[$valor[3]]; 
           } else { 
                 return FALSE; 
           } 
    } 

答案 1 :(得分:0)

更新:所以你有一个包含换行符的字符串。这个换行符是一个不可见的字符(\ n)。首先在每个换行符上拆分字符串。然后遍历这个数组。在每个逗号上拆分字符串并使用索引3更改值:

$myArray = explode("\n", $uploadServer);

$status = array(0  => "Unprocessed",
             1  => "Processing",
             2  => "Download aborted because the file became too big.",
             3  => "Download aborted because the file downloaded too long.",
             4  => "Download finished. Uploading to RapidShare.",
             5  => "Upload to RapidShare finished. Job finished.",
             7  => "Upload failed 3 times for unknown reasons. Job aborted.",
             8  => "Upload failed because the file is black listed.",
             9  => "Download failed for unknown reasons.",
             11 => "Enqueued for later processing because this account already downloads 5 files at the same time.");

$passer = array(); //create array to fill
foreach( $myArray as $valor ) { //loop through lines
   if( !empty($valor) ) { // check for empty line
     $tmp = explode(',', $valor);  //make array of strings
     if(array_key_exists($tmp[3],$status)) {  //check value [3]
       $tmp[3] = $status[$tmp[3]];
     }     
     $passer[] = $tmp;   //append array to parent array
  } 
}