English 中文(简体)
超过最大执行时间的多维数组
原标题:multidimensional array exceeding max execution time
  • 时间:2012-05-21 11:50:27
  •  标签:
  • php
  • arrays

我有一个 Csv 文件列表, 我需要 php 转换成一个大的多维数组, 测试仅用 2 php 超过执行第二次转换时的最大执行时间 。

第1个转换为从 csv 转换为数字多维数组

第2个转换是从数字多维数组转换为字符串和数字多维数组,而php就是在字符串和数字多维数组中产生错误的。

我的代码如下:

function parse($data) {
    $i = 0;
    while (isset($data[$i])) {
      $row = 0;
      while (isset($data[$i][$row])) {
        switch ($row) {
          case 0:
            //General Item details
            $fOv = 0;
            while (isset($data[$i][$row][$fOv])) {
              $array[ Item ][$i][$data[$i][row][$fOv]] = $data[$i][$row + 1][$fOv];
              $fOv++; // THIS IS THE PLACE OF THE ERROR
            }
            break;
          case 2:
            //General Transaction details
            $fOv = 0;
            while (isset($data[$i][$row][$fOv])) {
              $array[ Transaction ][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
              $fOv++;
            }
            break;
          case 4:
            //Buyer details
            $fOv = 0;
            while (isset($data[$i][$row][$fOv])) {
              $array[ Transaction ][ Buyer ][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
              $fOv++;
            }
            break;
          case 6:
            //Shipping details
            $fOv = 0;
            while (isset($data[$i][$row][$fOv])) {
              $array[ Transaction ][ Shipping ][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
              $fOv++;
            }
            break;
          case 8:
            //Status details
            $fOv = 0;
            while (isset($data[$i][$row][$fOv])) {
              $array[ Transaction ][ Status ][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
              $fOv++;
            }
            break;
        }
      $row + 2;
      }
      $i++;
    }
    return $array;
  }

这就是数字数组的样子:

array (
  0 => array (
    0 => array (
      0 =>  field 1 , 1 =>  field 2 , 2 =>  field 3  //continues to field 17
    )
    1 => array (
      0 =>  value 1 , 1 =>  value 2 , 2 =>  value 3  //continues to value 17
      //these values are for the fields in array[0][0]
    )
    2 => array ( //same as array[0][0] )
    3 => array ( //same as array[0][1], values for array[0][2] )
    4 => array ( //same as array[0][0] )
    5 => array ( //same as array[0][1], values for array[0][4] )
    6 => array ( //same as array[0][0] )
    7 => array ( //same as array[0][1], values for array[0][6] )
    8 => array ( //same as array[0][0] )
    9 => array ( //same as array[0][1], values for array[0][8] )
  )
  1 => array (
    //same structure as array[0], different details
  )
)

我想得到的字符串和数字数组:

array (
  0 => array (
     Item  => array (
      0 => array ( 
         field 1  =>  value 1 
         field 2  =>  value 2 
        //continues to field 17
      )
    )
     Transaction  => array (
       0 => array (
         field n  =>  value n  //repeated until field 17
         Buyer  => array (
          //same as above in array[0][ Item ][0]
        )
         Shipping  => array (
          //same as above in array[0][ Item ][0]
        )
         Status  => array (
          //same as above in array[0][ Item ][0]
        )
      )
    )
  )
  1 => array (
    //same structure as array[0], different values
  )
)

非常感谢任何帮助,事先感谢。

EDIT: I don t want to increase the max execution time, the script should take less than 30 seconds, the same as the working script I have which stores the data in the csv files in the first place.

最佳回答

快速和肮脏的方法:

ini_set( max_execution_time , 0);

虽然要知道如果脚本进入无限循环, 它永远不会结束 : () 更有可能的是, 取消时间限制会导致您进入一个内存限制, 所以您最好通过命令行运行, 以避免崩溃的缓冲 。 @ info: whatsthis

Edit: Try altering your code not to use variable names like $iii as this is very hard to read. You may find that this alone will make the solution obvious.

Edit: $row + 2; ought to be $row += 2;

问题回答

或您的 >php.ini 文件:

max_ execution_ time = 0 = 0





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签