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