0
我想从数组中删除一个对象,如果他的某个属性为null或空,这是代码。在数组上使用unset(),但它保留值
的阵列已经使用这个功能来分类:
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,但发送的数组是其中每个对象的整个数组。
我总是困惑,当我看到一个'为()'循环数组,而不是'的foreach()在' – 2015-07-12 22:50:48
PHP保留数字索引,你肯定有前项相同数量和之后? – spenibus