我不知道为什么,但是一些空白值仍会通过我的if
语句进行过滤...当我回显一个json编码版本时,某些值有“键”但“值”不包含任何东西。有什么想法吗?
function graph_fees($competitors, $fee_graph_data){
foreach ($competitors as $competitor) {
if (!empty($competitor->minSingleCareFee)) {
$fee_graph_data[$competitor->Home_name] = $competitor->minSingleCareFee;
}
}
return $fee_graph_data;
}
$mm_fees = graph_fees($competitors, $fee_graph_data);
echo json_encode($mm_fees);
答案 0 :(得分:0)
在创建数据时可能会出现空白,在检查之前修剪()它。
<?php
function graph_fees($competitors, $fee_graph_data = array())
{
foreach ($competitors as $competitor) {
$competitor->Home_name = trim($competitor->Home_name);
$competitor->minSingleCareFee = trim($competitor->minSingleCareFee);
if (!empty($competitor->minSingleCareFee) && !empty($competitor->Home_name)) {
$fee_graph_data[$competitor->Home_name] = $competitor->minSingleCareFee;
}
}
return $fee_graph_data;
}
$mm_fees = graph_fees($competitors, $fee_graph_data);
echo json_encode($mm_fees);
?>
答案 1 :(得分:0)
foreach ($competitors as $competitor) {
$myValue = trim($competitor->minSingleCareFee);
if (!empty($myValue)) {
$fee_graph_data[$competitor->Home_name] = $competitor->minSingleCareFee;
}
}
答案 2 :(得分:0)
包含空格的字符串不为空。截至php doc:
以下内容被认为是空的:
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)
因此,如果您的if中出现问题,$competitor->minSingleCareFee
确实包含某些内容。
尝试
if ( isset($competitor->minSingleCareFee)
&& trim($competitor->minSingleCareFee) != false ) {
会跳过" "
或"\n"