【发布时间】:2015-10-01 03:00:16
【问题描述】:
如果他的属性之一为空或空,我正在尝试从数组中删除一个对象,这就是代码。
数组已使用此函数排序:
function sortArray($c1, $c2)
{
return ($c1->propertyToCheck < $c2->propertyToCheck);
}
万一它改变了什么。
$myArray = array();
...
// Add values to the array here
...
usort($myArray,"sortArray");
for($i = 0; $i < count($myArray ); $i++)
{
if(empty($myArray[$i]->propertyToCheck))
{
unset($myArray[$i]);
// var_dump($myArray[$i]) returns NULL
}
}
echo json_encode($myArray);
// Returns the entire array, even with the values that shouldn't be there.
代码在函数内部,但数组是在所述函数内部创建的。
我正在使用 echo json_encode($myArray) 将值发送回 AJAX,但发送的数组是包含每个对象的整个数组。
【问题讨论】:
-
当我看到
for()在数组上循环而不是foreach()时,我总是感到困惑 -
php 保留数字索引,你确定前后项的数量相同吗?