PHP数组两次,最后值

时间:2016-02-26 01:44:49

标签: php arrays for-loop

我要获取一个数组以附加到字符串并在值为#34;"时忽略该值。不知何故,结果数组是这样的: Array ( [0] => test1 [1] => test2 [2] => t3 [3] => t4 [4] => t5 [5] => t6 ) test1,test2,t3,t4,t5,t6

php代码:

<?php
include ('connServer.php');

if(isset($_POST['submit']))
{
//$query = 

$updatedBy = $_POST['username'];
$shift = $_POST['shift'];
$date = $_POST['currDate'];
$machine = $_POST['machine'];
$timeDown = $_POST['timeDown'];
$timeUp = $_POST['timeUp'];
$downtimeType = $_POST['dtType'];
$tsDowntime = $_POST['troubleDT'];
$proDowntime = $_POST['proDowntime'];

function is_notnull($var) {return !is_null($var);}

switch ($downtimeType)
{
    case "Unscheduled":
    {
        $process = '-';
        $description = $_POST['description'];
        $actiontaken = $_POST['details'];
        $remark = '-';
        $sparepartstaken = $_POST['sparepartsTaken'];
    }break;

    case "PM":
    {
        $rebuiltParts = array();
        for($i = 1; $i <= 6; $i++)
        {
            $rebuiltParts[] = $_POST['rebuildParts'.$i];
            $rebuiltPartsfiltered = array_filter($rebuiltParts, "is_notnull");
        }
        print_r(array_filter($rebuiltParts, "is_notnull"));
        $process = '-';
        $description = "Rebuilt Parts:" .implode(',',$rebuiltPartsfiltered). "";
        $actiontaken = $_POST['actTaken'];
        $remark = '-';
        $sparepartstaken = $_POST['sparepartsTaken'];
    }break;

    case "Conversion":
    {
        $process = '-';
        $description = '' .$_POST['description']. '';
        $actiontaken = $_POST['actTaken'];
        $remark = '-';
        $sparepartstaken = $_POST['sparepartsTaken'];
    }break;

    case "Process_Issue":
    {
        $process = '-';
        $description = '' .$_POST['description']. '';
        $actiontaken = $_POST['actTaken'];
        $remark = '-';
        $sparepartstaken = $_POST['sparepartsTaken'];
    }break;

    case "Others":
    {
    }break;

    case "Bi-Weekly":
    {}break;

    default:
    {} break;
}
print_r(implode(',',$rebuiltPartsfiltered));
/*  
$process
$description
$actiontaken
$remark
$sparepartstaken
*/
}
?>

搜索并尝试了其他的东西,但它只会变得更糟。但我真的不知道如何在Google搜索中准确表达这一点。我做错了(我相信它可能是for循环)还是其他什么?或者有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

所以你的输出:

  

数组([0] =&gt; test1 [1] =&gt; test2 [2] =&gt; t3 [3] =&gt; t4 [4] =&gt; t5 [5] =&gt; t6)test1, TEST2,T3,T4,T5,T6

具有完美的感觉。你首先得到这个代码......

print_r(array_filter($rebuiltParts, "is_notnull"));

...正在打印出一个数组。这就产生了这个输出:

  

数组([0] =&gt; test1 [1] =&gt; test2 [2] =&gt; t3 [3] =&gt; t4 [4] =&gt; t5 [5] =&gt; t6)

然后在最后你有这个代码...

print_r(implode(',',$rebuiltPartsfiltered));

将数组值打印为以逗号分隔的字符串。这产生了这个输出:

  

为test1,TEST2,T3,T4,T5,T6