如何迭代3个不同大小的数组?

时间:2014-01-06 05:43:56

标签: php

我有3个阵列应该具有相同的尺寸,但有时它们不是。如何在环绕它们时用“空”值填充它们?

说我有这个:

array a -> { a , b , c};
array b -> { a , b , c, d};
array c -> { a , b , c, e, f};

输出应为:

array a -> { a , b , c, "empty", "empty", "empty"};
array b -> { a , b , c, d, "empty", "empty"};
array c -> { a , b , c, e, f};

我想要实现与这三个数组类似的东西:

$flightplan_pdf_files = glob("{$scandir}/{$project}/flightplans/*.{pdf}", GLOB_BRACE);
$flightplan_zip_files = glob("{$scandir}/{$project}/flightplans/*.{zip}", GLOB_BRACE);
$flightplan_kml_files = glob("{$scandir}/{$project}/flightplans/*.{kml}", GLOB_BRACE);

这是我的代码:

$flightplan_pdf_files = glob("{$scandir}/{$project}/flightplans/*.{pdf}", GLOB_BRACE);
$flightplan_zip_files = glob("{$scandir}/{$project}/flightplans/*.{zip}", GLOB_BRACE);
$flightplan_kml_files = glob("{$scandir}/{$project}/flightplans/*.{kml}", GLOB_BRACE);


echo "<table class=\"table-list-search table table-striped table-hover\">";
//i assume the arrays are all the same length, so i just iterate over the first one,
//but if it sizes are not the same, how can i insert "empty" into them to make sure they are the same size?


for ($i = 0; $i < count($flightplan_zip_files); $i++)
{
    $zip = pathinfo($flightplan_zip_files[$i]);
    $zip = $zip['filename'] . ".zip";               
    $kml = pathinfo($flightplan_kml_files[$i]);
    $kml = $kml['filename'] . ".zip";   
    $pdf = pathinfo($flightplan_pdf_files[$i]);
    $pdf = $pdf['filename'] . ".zip";


    echo "<tr>";
    echo "<td><span class=\"pull-left\"><img src=\"http://110.142.4.9/atlass/scp/zip.gif\" width=\"24\" height=\"24\"></span></td><td><span><a href=# onclick=\"test('" . $flightplan_zip_files[$i] . "');\">{$zip}</a></span></td>";   
    echo "<td><span class=\"pull-left\"><img src=\"http://110.142.4.9/atlass/scp/kml.jpg\" width=\"24\" height=\"24\"></span></td><td><span><a href=# onclick=\"test('" . $flightplan_kml_files[$i] . "');\">{$kml}</a></span></td>";
    echo "<td><span class=\"pull-left\"><img src=\"http://110.142.4.9/atlass/scp/downloadpdf.jpg\" width=\"24\" height=\"24\"></span></td><td><span><a href=# onclick=\"test('" . $flightplan_pdf_files[$i] . "');\">{$pdf}</a></span></td>";   
    echo "</tr>";
}


echo "</table>";

4 个答案:

答案 0 :(得分:1)

更改此行:

for ($i = 0; $i < count($flightplan_zip_files); $i++)

到此:

$length = max(count($flightplan_zip_files), count($flightplan_pdf_files), count($flightplan_kml_files));

for ($i = 0; $i < $length; $i++)

然后在每个pathinfo之前放置一个if条件,以便在需要时使用字符串`empty'。

还有其他方法可以做到这一点,但这只是其中之一。

答案 1 :(得分:1)

下面是我测试的脚本,以达到您所寻求的结果。在你的机器上试试吧..

基本上,首先我发现最大的三个数组。然后我从最大的数组中减去较小的数组,然后使用array_push,我将空数推入数组。

我提供此示例作为测试并给您提示的示例。请不要将此视为最终解决方案:)

<?php
$array1 = array('a','b','c');
$array2 = array('a','b','c','d');
$array3 = array('a','b','c','d','e');

$count_var1 = count($array1); 
$count_var2 = count($array2);
$count_var3 = count($array3);

if($count_var1 >= $count_var2 && $count_var1 >= $count_var3){
    $greatest = $count_var1;
}else if($count_var2 >= $count_var1 && $count_var2 >= $count_var3){
    $greatest = $count_var2;
}else if($count_var3 >= $count_var2 && $count_var3 >= $count_var3){
    $greatest = $count_var3;
}

$var_2_pad = $greatest-$count_var2;
$var_1_pad = $greatest-$count_var1;

for($i=1; $i<=$var_2_pad;$i++){
    $empty = 'empty';
    array_push($array2, $empty);
}

for($i=1; $i<=$var_1_pad;$i++){
    $empty = 'empty';
    array_push($array1, $empty);
}

echo "array2 = ";   print_r($array2);
echo "<br>";
echo "array3 = ";   print_r($array1);
echo "<br>";
echo "Original array = "; print_r($array3);

?>

答案 2 :(得分:0)

在访问$ flightplan _...变量中的任何内容之前,只需检查数组键是否存在?

if(array_key_exists($i, $flightplan_zip_files)) {
    ...
} else {
    $zip = "empty";
}

您也应该根据 bluesmoon的回答更新循环条件,以使其工作:

for($i = 0; $i < count($flightplan_zip_files) || $i < count($flightplan_pdf_files) || $i < count($flightplan_kml_files); $i++)

答案 3 :(得分:0)

小猪支持bluesmoon的回答。

您需要做的是找到最长数组的长度,然后多次迭代。

// find longest array
$arrays = array($flightplan_pdf_files, $flightplan_zip_files, $flightplan_kml_files);
$longest_length = max($arrays) // edited as per bluesmoon's suggestion

// Now $longest_length will hold the length of the longest array

// now loop
for ($i = 0; $i < $longest_length; $i++){

}