Issues when using multiple if statements in php
I needed suggestions for the following scenario.
I have a php variable called $data which holds a lot of information.I am trying to break up the data into fragments using the function explode. After exploding them into say n fragments, i loop through them and store around 50 lines in array1,50-100 lines in array2 and so on.
$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++;}
If i find that $arr["x10"] is not empty,then i print sample10.pdf.
If the above is not true ,then i check if $arr["x9"],if this is true then i print sample9.pdf.Like the following
if(!empty($arr["x10"])){template='https://www.xyz.com/folder/pdf/sample10.pdf';}
if(!empty($arr["x9"])){template='https://www.xyz.com/folder/pdf/sample9.pdf';}
if(!empty($arr["x8"])){template='https://www.xyz.com/folder/pdf/sample8.pdf';}
if(!empty($arr["x7"])){template='https://www.xyz.com/folder/pdf/sample7.pdf';}
if(!empty($arr["x6"])){template='https://www.xyz.com/folder/pdf/sample6.pdf';}
if(!empty($arr["x5"])){template='https://www.xyz.com/开发者_JS百科folder/pdf/sample5.pdf';}
if(!empty($arr["x4"])){template='https://www.xyz.com/folder/pdf/sample4.pdf';}
if(!empty($arr["x3"])){template='https://www.xyz.com/folder/pdf/sample3.pdf';}
if(!empty($arr["x2"])){template='https://www.xyz.com/folder/pdf/sample2.pdf';}
if(empty($arr["x2"])){template='https://www.xyz.com/folder/pdf/sample.pdf';}
I tried to execute this code,but the code just hangs and it gives me an error.I am trying to write a switch case but am not sure of how to proceed with that. Any help with the code would be appreciated. Thanks in advance.
This is begging for some judicious array usage (if not objects).
Firstly, you are breaking up your array in chunks of 45 skipping the first 27 elements. You can remove the first 27 elements using array_splice()
:
array_splice($data, 0, 27);
You then seem to create chunks of 45 elements. There's an inbuilt function for this called array_chunk()
.
$chunks = array_chunk($data, 45);
You're appending those chunks together with newlines. There's an easier way of doing that too:
$templates = array();
foreach ($chunks as $chunk) {
templates = implode("\n", $chunk);
}
but you can do better using array_map()
:
function concat(array $arr) {
return implode("\n", $arr);
}
$data = array_map('concat', $chunks);
Putting that altogether turns most of this code into a few lines (assuming concat()
):
$data = explode("\n", $data, 388);
array_splice($data, 0, 27);
$data = array_map('concat', $data);
From that array you should be able to figure out which is the relevant missing entry and print a sample based on the index number in the array. No need for a complicated series of if
s at all.
To give you an example:
$text = <<<END
one
two
three
four
five
six
seven
eight
END;
$data = explode("\n", $text);
print_r($data);
Output:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => five
[5] => six
[6] => seven
[7] => eight
)
then:
$data = array_chunk($data, 3);
print_r($data);
Output:
Array
(
[0] => Array
(
[0] => one
[1] => two
[2] => three
)
[1] => Array
(
[0] => four
[1] => five
[2] => six
)
[2] => Array
(
[0] => seven
[1] => eight
)
)
and finally:
function concat(array $arr) {
return implode("\n", $arr);
}
$data = array_map('concat', $data);
print_r($data);
Output:
Array
(
[0] => one
two
three
[1] => four
five
six
[2] => seven
eight
)
First off, why don't you try something like this:
$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';
}
}
... but I'm not sure what you're trying to do with the "template=" line. It's not valid PHP, so I don't know what it's supposed to do.
The problem is template
isn't a variable -- at the very least, you're not defining it as such in your code. Though your general approach here is somewhat misguided, I believe it will work if you change template
to $template
(note the $
)
First of all, those if's can be reduced to a loop (using 1,2... instead of x1, x2...)
$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";
}
I think that the last part with $template won't do what you're looking for, but that's what you have there.
I presume you are storing $data[0] through $data[26] in other variables?
One suggestion is to map the index values from $data to $arr using the formula
$i = ($x - 27)/45 + 1
,
where $x is the index value in $data and $i is the index number you wish to use in $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";
}
}
a modest suggestion, but I hope it helps.
精彩评论