Php 检查项目组中是否至少有一个项目不为空
我有这样的变量列表:Php 检查项目组中是否至少有一个项目不为空,php,Php,我有这样的变量列表: $item[1]['Value'] $item[2]['Value'] ... $item[50]['Value'] 其中一些为null 如何检查其中是否至少有一个不是空的,在本例中,如何将该变量(第一个或最后一个)定义为新变量 另外,$var[number]['foo']格式的名称是什么?它是数组吗?使用empty()函数的简单测试 if(!empty($item[$i]['value']) { echo "exists"; }else{ echo "is
$item[1]['Value']
$item[2]['Value']
...
$item[50]['Value']
其中一些为null
如何检查其中是否至少有一个不是空的,在本例中,如何将该变量(第一个或最后一个)定义为新变量
另外,
$var[number]['foo']
格式的名称是什么?它是数组吗?使用empty()
函数的简单测试
if(!empty($item[$i]['value'])
{
echo "exists";
}else{
echo "is not set";
}
如果要在新值不存在或未定义时影响它,可以使用null合并运算符
$item[$i]['value'] = $item[$i]['value'] ?? $newvalue;
要检查其中至少一个不是空的
<?php
$atleast=0;//all are empty
for($i=0;$i<count($item);$i++)
{
if(!empty($item[$i]['value']))
$atleast=1;//one is empty;
}
if($atleast==1)
echo "there is more than one element which are not empty";
else
echo "all are emtpy";
?>
这将最后找到的已使用元素设置为
newVariable
for($i = 0; $i < sizeOf($item); i ++)
{
if(isset($item[$i]['Value']))
{
$newVariable = $item[$i]['Variable'];
}
}
for($i=0;$i
您可以这样使用:
// example data
$items = [
['Value' => null],
['Value' => 2],
['Value' => null],
];
// remove elements with 'Value' = null
$filtered = array_filter($items, function($a) { return !is_null($a['Value']); }) ;
$element = null;
// If there is some values inside $filtered,
if (!empty($filtered)) {
// get the first element
$element = reset($filtered) ;
// get its value
$element = $element['Value'];
}
if ($element)
echo $element ;
将提供以下产出:
2
- 返回具有非空值的数组(使用use函数)
- 检查数组是否为空
- 获取数组中的第一个元素李>
$item[1]['Value'] = "A";
$item[2]['Value'] = "B";
$item[4]['Value'] = "";
$item[3]['Value'] = NULL;
foreach ($item as $key => $value) {
if (!empty($value['Value'])) {
echo "$key It's not empty \n";
} else {
echo "$key It's empty \n";
}
}
结果:
1 It's not empty
2 It's not empty
3 It's empty
4 It's empty
At least one item is empty
您也可以看到函数认为NULL为空。< /P> ----编辑---
我忘了添加方法来检测是否至少有一项是空的,您可以使用: 示例$item[1]['Value'] = "A";
$item[2]['Value'] = "B";
$item[4]['Value'] = "";
$item[3]['Value'] = NULL;
foreach ($item as $key => $value) {
if (!empty($value['Value'])) {
$item_result = false;
} else {
$item_result = true;
}
}
if ($item_result) {
echo "At least one item is empty";
} else {
echo "All items have data";
}
结果:
1 It's not empty
2 It's not empty
3 It's empty
4 It's empty
At least one item is empty
如果要将内键用作变量名,则可以检查是否可以创建它,如果可以,则可以创建它:
function any($array) {
foreach ($array as $key => $item) {
if ($item) return $key;
}
return false;
}
$variableName = any($var);
if ($variableName !== false) {
{$variableName} = "yourvalue";
} else {
//We ran out of variable names
}
另外,$var[number]['foo']
的格式是一个外部带有数字键的数组,内部带有字符串键的数组
<?php
$data =
[
1 => ['foo' => ''],
2 => ['bar' => 'big'],
10 => ['foo' => 'fat']
];
$some_foo = function ($data) {
foreach($data as $k => $v)
if(!empty($v['foo'])) return true;
return false;
};
var_dump($some_foo($data));
$data[10]['foo'] = null;
var_dump($some_foo($data));
或者,您可以筛选计算结果为false的foo值:
$some_foo = (bool) array_filter(array_column($data, 'foo'));
什么意思?如果
$item[34]
不是空的,$newOne=$item[34]
?是吗?循环全部并检查是否为空,是否给出了值?@litilion是的。请打印($item)
并向我们显示结果或部分结果,其中一些值不是空的,一些值是空的。不确定循环是否是最佳解决方案,而且项数未知,可能是1,可能是50,可能是100。但是,$i
没有定义。但是我不知道存在多少。可能有50个,我只想检查是否至少有一个不是空的,然后选择它