【问题标题】:How to write if all values in the array are null using an array filter如何使用数组过滤器写入数组中的所有值是否为空
【发布时间】:2016-05-19 20:45:04
【问题描述】:

如果客户尚未选择产品,我试图在视图中显示错误消息,该产品是数据库中的布尔值。这当前会显示消息,但一旦选定的列为真,它也会保持不变。

<?php
$company1 = DB::table('customers')->select('post', 'telans','conum')->where('business', $user )->get();
print_r($company1);
?>

@if (array_filter($company1))

<div class="alert alert-danger" role="alert">
Please <strong><a href="/user_register">click here</a></strong> to select your products</strong>
</div>
@endif 

如果有帮助,数组打印如下:

数组([0] => stdClass 对象([post] => [telans] => [conum] =>))

【问题讨论】:

  • 可以使用array_filter(),参考php.net/manual/en/function.array-filter.php
  • 我现在有了更好的理解。所以该函数将检查 NULL
  • 它会过滤数组值并返回新数组,所以你可以做类似count(array_filter($myarr))
  • 我看到它就想踢自己,但你能提供一个例子吗?

标签: php arrays laravel laravel-5.2


【解决方案1】:

回答

我需要删除对象 stdClass 并进行一些调整。

<?php
function checkIsset($val) {
    return count(array_filter($val));
}

$company1 = DB::table('customers')->select('post', 'telans','conum')->where('business', $user )->get();

$array = json_decode(json_encode($company1), true);

$filteredArr = array_filter($array, 'checkIsset');

print_r( $array);
//dd($filteredArr);
?>
@if (count(array_filter($filteredArr)) == 0)
@if ( !is_null($company)) 


<div class="alert alert-danger" role="alert">
Please <strong><a href="/user_register">click here</a></strong> to select your products</strong>
</div>
@endif 
@endif 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多