我有一個數組那是我在會議lifeFleetSelectedTrucksList
攜帶我也有類的對象一個對象fleetUnit
從陣列中移除物體;如何避免空
class fleetUnit {
public $idgps_unit = null;
public $serial = null;
}
class lifeFleetSelectedTrucksList {
public $arrayList = array();
}
$listOfTrucks = new lifeFleetSelectedTrucksList(); //this is the array that I carry in session
if (!isset($_SESSION['lifeFleetSelectedTrucksList'])) {
$_SESSION['lifeFleetSelectedTrucksList'] == null; //null the session and add new list to it.
} else {
$listOfTrucks = $_SESSION['lifeFleetSelectedTrucksList'];
}
我使用它來從數組中刪除元素:
$listOfTrucks = removeElement($listOfTrucks, $serial);
這是我的功能,刪除元素並返回沒有元素的數組:
function removeElement($listOfTrucks, $remove) {
for ($i = 0; $i < count($listOfTrucks->arrayList); $i++) {
$unit = new fleetUnit();
$unit = $listOfTrucks->arrayList[$i];
if ($unit->serial == $remove) {
unset($listOfTrucks->arrayList[$i]);
break;
} elseif ($unit->serial == '') {
unset($listOfTrucks->arrayList[$i]);
}
}
return $listOfTrucks;
}
嗯,它的工作原理 - 元素被刪除,但我有數組,它有一堆空vaues代替。我如何返回不包含空元素的數組?似乎我並沒有起訴正確的東西。
使用http://php.net/manual/en/function.array-filter.php –