【发布时间】:2016-02-03 16:38:44
【问题描述】:
如果我的问题很愚蠢,请原谅我。但请有人告诉我或建议我如何解决这个问题。其实我有很大的多维数组
喜欢这个
$array_value = Array
(
[0] => Array
(
[0] => sdfsf
[1] => fghbfh
[2] => sgddfsg
[3] => ujmnm
[4] => jkluik
..
..
..
..
..
..
[150] => jhbjhbjh
)
[1] => Array
(
[0] => 44062
[1] => 45503
[2] => 44062
[3] => fdg
[4] => dfgdg
..
..
..
..
..
..
[150] => jhbjhbjh
)
....
....
....
[590] => Array
(
[0] => 44062
[1] => 45503
[2] => 44062
[3] => fdg
[4] => dfgdg
..
..
..
..
..
..
[150] => jhbjhbjh
)
)
600 个数组在 600 个数组中我有 150 个数组值。当我使用这个数组 if foreach 条件和 for 循环条件时,完成执行需要超过 5 分钟。
我在循环中使用了两个 foreach 循环和一个 for 循环和 4 个方法函数。执行需要超过 5 分钟。我需要更快地加载。
我不知道如何增加脚本的执行时间或如何处理这个数组。请有人建议或帮助我如何处理这个问题。谢谢你。
foreach ($Final_Data as $line_no => $val) {
foreach($val as $col_name=> $col_value) {
if ($col_name === 'tid' || $col_name === 'pid' || $col_name === 'local_list_id')
{
if(empty($col_value)) {
$null_error[] = "empty value in ".$col_name;
}elseif( $col_value !== null && !is_numeric( $col_value) ) {
$where_Error[] ="non numeric value in ".$col_name." = ".$col_value;
}
$where_content[]= $col_name . "= '" . $col_value."'";
if($col_name =='pid'){
$pid = $col_value;
}
}elseif(!empty($col_name) && in_array($col_name, $update))
{
if($col_name=="country"){
...
}elseif($col_name=="state"){
...
}elseif($col_name=="district"){
....
}elseif($col_name=="post_category")
{
....
$CategoryList=array();
if(count($locationArr) > 0 && count(locationCategory($locationArr)) > 0)
{
.....
}
if(strlen($col_value)==0 || empty($col_value))
{
......
}elseif(ValidateLength($col_name,$columnValue,$maxl) === false || VulnerableExists($columnValue)===false)
{
if(ValidateLength($col_name, $columnValue,$maxl) === false){
....
}
if(VulnerableExists($columnValue)===false){
....
}
}else
{
......
foreach($post_cat as $Category){
if(get_cat_ID($Category) == 0) {
if(!in_array($Category, $CategoryList)){
.....
}
}
}
/*****Category Mapping Start****/
if(count($post_cat)>0 && isset($pid) && !empty($pid) && count($CategoryList)==0)
{
$postCat = array();
$ex_catid = array();
$post_categories = get_the_category( $pid );
foreach($post_categories as $category) {
if(in_array($category->name, $post_cat)){
.....
}
}
$array_dif = array_diff($post_cat,$postCat);
foreach($array_dif as $pc){
......
if($cat_Id!=0) array_push($ex_catid,$cat_Id);
}
if(count($array_dif)!=0){
......
}
}elseif(count($CategoryList)>0)
{
}
}
}
$columnName=$col_name;
$columnValue=mysqli_real_escape_string($link, $col_value);
if(ValidateLength($col_name,$col_value,$maxl)===false)
{
......
}elseif(VulnerableExists($col_value)===false)
{
......
}else
{
......
}
}
}
die();
// echo implode(", " ,$where_content)."<br>";
// echo implode(", " ,$update_content);
if(!empty($val['pid']) && !empty($val['tid']) && !empty($val['local_list_id']) ) {
if(count($vuln_error) <= 0 && count($length_error)<=0 && count($null_error)<=0 && count($CategoryList)<=0){
....
}else
{
....
}
}
//die();
unset($where_content);
unset($update_content);
unset($null_error);
unset($vuln_error);
unset($length_error);
unset($CategoryList);
echo "<br><br>";
}
我只给出了我的代码的基本结构。这就是我的整个脚本如何在 forloop 和 foreach 条件中包含许多 if 条件。但所有条件都是强制性的,因为它是为了验证和一些操作。请有人帮我解决这个问题。谢谢你
这个数组中最重要的事情和问题是值是 1000 个字符和超过 1000 个字符。在这里,我刚刚给出了 4 个数字和 5 个字符串。
【问题讨论】:
-
你知道每个小数组中“列”的总数吗?数组键总是简单的索引号吗?
-
validation_funtion 工作是什么?你在哪里使用 $value
-
@Munna 感谢您的回答。我进行了一些测试,并且像您描述的那样解析数组非常快。让你的脚本变慢的实际上是 if {} else {} 语句和验证函数。我建议您将最常发生的条件放入 if() 条件中。基本示例: if (a==b) { /*在这里写出比else语句更频繁发生的代码*/}
-
如果您发布“验证”流程,我们可能会为您提供帮助。只需从您的代码中删除敏感内容并将其发布在此处。在我看来,您需要改进的地方。
-
我怀疑 'in_array' 值得一看。 150 数组条目中的索引如何映射到列? imo,我怀疑验证会更有用,因为数组中的函数被传递了适当的数据来检查,而不是
nested if else statments。我需要更多关于... } elseif ($col_name=="district") {...会发生什么的信息。
标签: php arrays multidimensional-array foreach