Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用多个if语句时出现的问题_Php - Fatal编程技术网

在php中使用多个if语句时出现的问题

在php中使用多个if语句时出现的问题,php,Php,我需要以下场景的建议 我有一个名为$data的php变量,它保存了大量信息。我试图使用explode函数将数据分解成片段。 在将它们分解成n个片段后,我循环遍历它们,并在array1中存储大约50行,在array2中存储50-100行,以此类推 $data = explode( "\n" , $data , 388 ); $x = 27; while( $x < 72 and $x>=27){$arr["x2"] .= $data[$x] . "\n"; $x++;}

我需要以下场景的建议

我有一个名为$data的php变量,它保存了大量信息。我试图使用explode函数将数据分解成片段。 在将它们分解成n个片段后,我循环遍历它们,并在array1中存储大约50行,在array2中存储50-100行,以此类推

   $data = explode( "\n" , $data , 388 );

$x = 27;
while( $x < 72 and $x>=27){$arr["x2"] .= $data[$x] . "\n";
    $x++;}

$x = 72;
while( $x < 117 and $x>=72){$arr["x3"] .= $data[$x] . "\n";
    $x++;}

$x = 117;
while( $x < 162 and $x>=117){$arr["x4"] .= $data[$x] . "\n";
    $x++;}

$x = 162;
while( $x < 207 and $x>=162){$arr["x5"] .= $data[$x] . "\n";
    $x++;}

$x = 207;
while( $x < 252 and $x>=207){$arr["x6"] .= $data[$x] . "\n";
    $x++;}

$x = 252;
while( $x < 297 and $x>=252){$arr["x7"] .= $data[$x] . "\n";
    $x++;}

$x = 297;
while( $x < 342 and $x>=297){$arr["x8"] .= $data[$x] . "\n";
    $x++;}

$x = 342;
while( $x < 387 and $x>=342){
    $arr["x9"] .= $data[$x] . "\n";
    $x++;}

$x = 387;
if(!empty($data[$x])){$arr["x10"] .= $data[$x];
        $x++;}
我试图执行这段代码,但代码只是挂起,它给了我一个错误。我试图写一个开关案例,但不知道如何继续。 任何代码方面的帮助都将不胜感激。
提前感谢。

这是在乞求明智地使用数组(如果不是对象的话)

首先,将数组分成45个块,跳过前27个元素。可以使用以下方法删除前27个图元:

然后,您似乎创建了45个元素的块。这个函数有一个内置函数,叫做

您正在用换行符将这些块附加在一起。还有一种更简单的方法:

$templates = array();
foreach ($chunks as $chunk) {
  templates = implode("\n", $chunk);
}
但您可以通过以下方式做得更好:

将这些代码放在一起,将大部分代码转换为几行(假设
concat()
):

从该数组中,您应该能够找出哪个是相关的缺失条目,并根据数组中的索引号打印样本。根本不需要复杂的
if
s序列

举个例子:

$text = <<<END
one
two
three
four
five
six
seven
eight
END;
$data = explode("\n", $text);
print_r($data);
然后:

输出:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => eight
)
Array
(
    [0] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

    [1] => Array
        (
            [0] => four
            [1] => five
            [2] => six
        )

    [2] => Array
        (
            [0] => seven
            [1] => eight
        )

)
Array
(
    [0] => one
two
three
    [1] => four
five
six
    [2] => seven
eight
)
最后:

function concat(array $arr) {
  return implode("\n", $arr);
}

$data = array_map('concat', $data);
print_r($data);
输出:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => eight
)
Array
(
    [0] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

    [1] => Array
        (
            [0] => four
            [1] => five
            [2] => six
        )

    [2] => Array
        (
            [0] => seven
            [1] => eight
        )

)
Array
(
    [0] => one
two
three
    [1] => four
five
six
    [2] => seven
eight
)

问题是,
模板
不是一个变量——至少在代码中没有这样定义它。虽然您在这里的一般方法有些误导,但我相信如果您将
模板更改为
$template
(请注意
$

首先,您为什么不尝试这样的方法呢:

$data = explode( "\n" , $data , 388 );

for ($x = 27; $x < 342; $x++){
   $subarr = (($x-27)/50)+2;
   $arr['x'.$subarr] .= $data[$x] . "\n";
}

for ($i = 2; $i <= 10; $i++) {
    if (!empty($arr['x'.$i])) {
        template='https://www.xyz.com/folder/pdf/sample'.$i.'.pdf';
    }
}
$data=explode(“\n”,$data,388);
对于($x=27;$x<342;$x++){
$subar=($x-27)/50)+2;
$arr['x'.$subar].=$data[$x]。“\n”;
}

对于($i=2;$i首先,那些if可以简化为一个循环(使用1,2…而不是x1,x2…)

$arr=Array();
对于($i=0;$data[$i];i++){
对于($j=0;$j<10;$j++){
$arr[i]。=$data[50*$i+$j]。“\n”;
}
}
foreach($arr作为$key=>$val){
$template=”https://www.xyz.com/folder/pdf/sample$key.pdf“;
}

我认为带有$template的最后一个部分不能满足您的要求,但这就是您所拥有的。

我想您是在其他变量中存储$data[0]到$data[26]吗

一个建议是使用公式将索引值从$data映射到$arr

$i=($x-27)/45+1

其中,$x是$data中的索引值,$i是希望在$arr中使用的索引号

for($x=27;$x<388;$x++)
{
   $i = ($x - 27)/45 + 1;
   if (isset($arr["x".$i]))
   {
     $arr["x".$i] .= $data[$x] . "\n";
   }
   else
   {
     $arr["x".$i] = $data[$x] . "\n";
   }
 }

用于($x=27;$x偏移量是否有意义?即$data中的信息是否总是结构化的?是的。它将包含字母数字字符。如果您不希望其他字符执行,您可能应该使用
else if
而不是if。否则,您可能会出现两个或多个打印语句。哦,请使用sane
>for()
construct…在这之前添加一个
array_拼接($data,0,28);
。你说得对。我忘了在模板前面添加$,在这里
Array
(
    [0] => one
two
three
    [1] => four
five
six
    [2] => seven
eight
)
$data = explode( "\n" , $data , 388 );

for ($x = 27; $x < 342; $x++){
   $subarr = (($x-27)/50)+2;
   $arr['x'.$subarr] .= $data[$x] . "\n";
}

for ($i = 2; $i <= 10; $i++) {
    if (!empty($arr['x'.$i])) {
        template='https://www.xyz.com/folder/pdf/sample'.$i.'.pdf';
    }
}
$arr = Array();
for( $i=0;$data[$i];i++) {
    for ($j = 0; $j < 10; $j++) {
        $arr[i].= $data[50*$i+$j] . "\n";
    }
}

foreach($arr as $key => $val) {
    $template="https://www.xyz.com/folder/pdf/sample$key.pdf";
}
for($x=27;$x<388;$x++)
{
   $i = ($x - 27)/45 + 1;
   if (isset($arr["x".$i]))
   {
     $arr["x".$i] .= $data[$x] . "\n";
   }
   else
   {
     $arr["x".$i] = $data[$x] . "\n";
   }
 }