Sadece bazı iyi cevaplar, ama bir dizi boş ise PHP belirlediğinde daha net açıklamak için biraz genişletmek düşündüm.
Ana Notlar:
Bir anahtar (ya da anahtarlar) ile bir dizi PHP ile NOT empty olarak belirlenecektir.
Dizi değerleri tuşları mevcut ihtiyaç olarak boş ise, bir dizi değerleri olan ya da olmayan hiçbir tuşa (ve dolayısıyla hiçbir değer) vardır sadece belirlemek değil.
Yani değerleri veya varsa empty()
sadece size değil, bir dizi kontrol, dizi boşsa o size söyler, ve anahtarlar bir dizi parçasıdır.
So consider how you are producing your array before deciding which checking method to use.
EG An array will have keys when a user submits your HTML form when each form field has an array name (ie name="array[]"
).
A non empty array will be produced for each field as there will be auto incremented key values for each form field's array.
Örneğin bu dizileri atın:
/* Assigning some arrays */
// Array with user defined key and value
$ArrayOne = array("UserKeyA" => "UserValueA", "UserKeyB" => "UserValueB");
// Array with auto increment key and user defined value
// as a form field would return with user input
$ArrayTwo[] = "UserValue01";
$ArrayTwo[] = "UserValue02";
// Array with auto incremented key and no value
// as a form field would return without user input
$ArrayThree[] = '';
$ArrayThree[] = '';
Eğer yukarıdaki diziler için dizi anahtarları ve değerleri echo varsa, şu olsun:
ARRAY ONE:
[UserKeyA] => [UserValueA]
[UserKeyB] => [UserValueB]
ARRAY TWO:
[0] => [UserValue01]
[1] => [UserValue02]
ARRAY THREE:
[0] => []
[1] => []
Ve empty()
ile yukarıdaki dizileri test aşağıdaki sonuçları verir:
ARRAY ONE:
$ArrayOne is not empty
ARRAY TWO:
$ArrayTwo is not empty
ARRAY THREE:
$ArrayThree is not empty
Eğer bir dizi atayın ama gibi, bundan sonra kullanmak değil ne zaman bir dizi her zaman boş olacaktır:
$ArrayFour = array();
Eğer empty()
yukarıdaki kullanırken bu boş olacak, yani PHP TRUE döndürecektir.
Diziniz tuşlara sahiptir - yani örneğin bir formun girdi isimleri veya bunları el atarsanız (yani veritabanından tuşları ama hiçbir değerler / veri olarak veritabanı sütun adları ile bir dizi oluşturmak), ya sonra dizi {olmayacaktır [ (0)]}.
Bu durumda, her anahtar bir değeri varsa, test döngü foreach dizi olabilir. Belki tuşları kontrol veya veri sanitasyon, yine dizi üzerinden çalıştırmak gerekirse bu iyi bir yöntemdir.
However it is not the best method if you simply need to know "if values exist" returns TRUE or FALSE.
There are various methods to determine if an array has any values when it's know it will have keys. A function or class might be the best approach, but as always it depends on your environment and exact requirements, as well as other things such as what you currently do with the array (if anything).
İşte bir dizi değerlerini olup olmadığını kontrol etmek çok az kod kullanan bir yaklaşım:
Using array_filter()
:
Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
$EmptyTestArray = array_filter($ArrayOne);
if (!empty($EmptyTestArray))
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
array_filter()
aşağıdaki (bu cevap ilk kod bloğu oluşturulan) Tüm üç örnek diziler sonuçları üzerinde çalışan:
ARRAY ONE:
$arrayone is not empty
ARRAY TWO:
$arraytwo is not empty
ARRAY THREE:
$arraythree is empty
So when there are no values, whether there are keys or not, using array_filter()
to create a new array and then check if the new array is empty shows if there were any values in the original array.
It is not ideal and a bit messy, but if you have a huge array and don't need to loop through it for any other reason, then this is the simplest in terms of code needed.
Ben giderlerini kontrol deneyimli değilim, ancak kullanmadan array_filter()
ve bir değer bulunursa foreach
kontrol arasındaki farkları bilmek iyi olurdu.
Açıkçası kriter küçük ve büyük dizilerde çeşitli parametreler, üzerinde olması gerekecek ve değerleri değil, vs varken