我想破坏for循环中的多数组。
$_POST['PprodName'];
在这个$_POST['PprodName']
中,我的值为:
Array ( [0] => steel mj23 [1] => [2] => [3] => [4] => [5] => [6] => [7] => )
steel mj23
是我的第一个产品名称。
现在我想根据位置检查元素是否为空。所以我应用了一个for循环,但不知道如何破坏非空的元素。
我想只破坏那些非空的元素。
这是我的for循环。
for( $i=0; $i < count($_POST['PprodName']); $i++ ) {
if( !empty( $_POST['PprodName'][$i] ) ) {
print_r($_POST['PprodName'][$i]);
}
}
答案 0 :(得分:8)
您不需要for循环。只需使用array_filter()
过滤所有空值,然后您就可以使用implode()
,如下所示:
echo implode(", ", array_filter($_POST['PprodName']));
答案 1 :(得分:0)
试试这种方式 Online Demo
$data=Array ( 0 => "steel mj23", 1 =>'test' ,2 =>'', 3 =>'' ,4 =>"", 5 =>"", 6 =>"", 7 =>"" );
$result=Array();
foreach($data as $key=>$value)
{
if(!empty($value))
$result[]=$value;
}
var_dump($result);
答案 2 :(得分:0)
<div data-role="page" id="AppBody" style="background: #00AEEF">
<div data-role="header" style="background:#0E74BC;color:white;">
<h1 class="Home">Home</h1>
<a href="#" data-role="button" data-direction="reverse" onclick="WL.Client.logout('CustomAuthenticatorRealm',{onSuccess: WL.Client.reloadApp})" data-transition="slide" data-iconpos="notext" class="ui-btn-right">
<img src="images/logout.png" style="width: 25px;">
</a>
</div>
<div role="main" class="ui-content">
<div class="ui-grid-a">
<!-- menu-container -->
<div class="ui-block-a">
<div class="ui-bar ui-bar-a">
<div class="menu-elem">
<a href="#">
<div class="menu-img">
<img src="http://placehold.it/50x50" style="width: 50px;">
</div>
<div class="menu-text">test</div>
</a>
</div>
</div>
</div>
</div>
</div>
</div>