として von der OP angegeben :
PHP behandelt alle Arrays als assoziativ
ist es (IMHO) nicht ganz sinnvoll, eine Funktion zu schreiben, die prüft, ob ein Array assoziativ . Also, das Wichtigste zuerst: Was ist ein Schlüssel in einem PHP-Array? ?:
El Schlüssel kann entweder ein Ganzzahl oder eine String .
Das bedeutet, dass es 3 mögliche Fälle gibt:
- Fall 1. alle Schlüssel sind numerisch / Ganzzahlen .
- Fall 2. alle Schlüssel sind Zeichenketten .
- Fall 3. Einige Schlüssel sind Zeichenketten sind einige Schlüssel numerisch / Ganzzahlen .
Wir können jeden Fall mit den folgenden Funktionen überprüfen.
Fall 1: Alle Schlüssel sind numerisch / Ganzzahlen .
Hinweis : Diese Funktion gibt zurück wahr auch für leere Arrays.
//! Check whether the input is an array whose keys are all integers.
/*!
\param[in] $InputArray (array) Input array.
\return (bool) \b true iff the input is an array whose keys are all integers.
*/
function IsArrayAllKeyInt($InputArray)
{
if(!is_array($InputArray))
{
return false;
}
if(count($InputArray) <= 0)
{
return true;
}
return array_unique(array_map("is_int", array_keys($InputArray))) === array(true);
}
Fall 2: Alle Schlüssel sind Zeichenketten .
Hinweis : Diese Funktion gibt zurück wahr auch für leere Arrays.
//! Check whether the input is an array whose keys are all strings.
/*!
\param[in] $InputArray (array) Input array.
\return (bool) \b true iff the input is an array whose keys are all strings.
*/
function IsArrayAllKeyString($InputArray)
{
if(!is_array($InputArray))
{
return false;
}
if(count($InputArray) <= 0)
{
return true;
}
return array_unique(array_map("is_string", array_keys($InputArray))) === array(true);
}
Fall 3. Einige Schlüssel sind Zeichenketten sind einige Schlüssel numerisch / Ganzzahlen .
Hinweis : Diese Funktion gibt zurück wahr auch für leere Arrays.
//! Check whether the input is an array with at least one key being an integer and at least one key being a string.
/*!
\param[in] $InputArray (array) Input array.
\return (bool) \b true iff the input is an array with at least one key being an integer and at least one key being a string.
*/
function IsArraySomeKeyIntAndSomeKeyString($InputArray)
{
if(!is_array($InputArray))
{
return false;
}
if(count($InputArray) <= 0)
{
return true;
}
return count(array_unique(array_map("is_string", array_keys($InputArray)))) >= 2;
}
Daraus folgt, dass:
Damit ein Array zu einem "echte" Reihe an die wir uns alle gewöhnt haben, das heißt:
- Seine Tasten sind alle numerisch / Ganzzahlen .
- Seine Schlüssel sind sequenziell (d. h. Erhöhung um Stufe 1).
- Seine Tasten bei Null anfangen .
Wir können dies mit der folgenden Funktion überprüfen.
Fall 3a. Tasten sind numerisch / Ganzzahlen , sequenziell y Null-Basis .
Hinweis : Diese Funktion gibt zurück wahr auch für leere Arrays.
//! Check whether the input is an array whose keys are numeric, sequential, and zero-based.
/*!
\param[in] $InputArray (array) Input array.
\return (bool) \b true iff the input is an array whose keys are numeric, sequential, and zero-based.
*/
function IsArrayKeyNumericSequentialZeroBased($InputArray)
{
if(!is_array($InputArray))
{
return false;
}
if(count($InputArray) <= 0)
{
return true;
}
return array_keys($InputArray) === range(0, count($InputArray) - 1);
}
Caveats / Pitfalls (oder noch mehr seltsame Fakten über Array-Schlüssel in PHP)
Ganzzahlige Schlüssel
Die Schlüssel für diese Arrays sind Ganzzahlen :
array(0 => "b");
array(13 => "b");
array(-13 => "b"); // Negative integers are also integers.
array(0x1A => "b"); // Hexadecimal notation.
String-Tasten
Die Schlüssel für diese Arrays sind Zeichenketten :
array("fish and chips" => "b");
array("" => "b"); // An empty string is also a string.
array("stackoverflow_email@example.com" => "b"); // Strings may contain non-alphanumeric characters.
array("stack\t\"over\"\r\nflow's cool" => "b"); // Strings may contain special characters.
array('$t€køvrflöw' => "b"); // Strings may contain all kinds of symbols.
array("functon" => "b"); // You think this looks fine? Think again! (see https://stackoverflow.com/q/9246051/1402846)
array("" => "b"); // How about Japanese/Korean/Chinese/Russian/Polish?
array("fi\x0sh" => "b"); // Strings may contain null characters.
array(file_get_contents("https://www.google.com/images/nav_logo114.png") => "b"); // Strings may even be binary!
Ganzzahlige Schlüssel, die wie Zeichenketten aussehen
Wenn Sie glauben, dass der Schlüssel in array("13" => "b")
est un String , Sie sind falsch . Aus dem Dokument aquí :
Zeichenketten, die gültige Ganzzahlen enthalten, werden in den Typ Ganzzahl umgewandelt. So wird z. B. der Schlüssel "8" tatsächlich unter 8 gespeichert. Andererseits wird "08" nicht umgewandelt, da es sich nicht um eine gültige Dezimalzahl handelt.
Die Schlüssel für diese Arrays sind zum Beispiel Ganzzahlen :
array("13" => "b");
array("-13" => "b"); // Negative, ok.
Der Schlüssel für diese Arrays sind jedoch Zeichenketten :
array("13." => "b");
array("+13" => "b"); // Positive, not ok.
array("-013" => "b");
array("0x1A" => "b"); // Not converted to integers even though it's a valid hexadecimal number.
array("013" => "b"); // Not converted to integers even though it's a valid octal number.
array("18446744073709551616" => "b"); // Not converted to integers as it can't fit into a 64-bit integer.
Darüber hinaus ist nach Angaben der doc ,
Die Größe einer ganzen Zahl ist plattformabhängig, obwohl ein Maximalwert von etwa zwei Milliarden der übliche Wert ist (das sind 32 Bits mit Vorzeichen). Bei 64-Bit-Plattformen liegt der Höchstwert normalerweise bei etwa 9E18, außer bei Windows, das immer 32 Bit hat. PHP unterstützt keine vorzeichenlosen Ganzzahlen.
Der Schlüssel für dieses Array ist also kann, muss aber nicht ein sein Ganzzahl - das hängt von Ihrer Plattform ab.
array("60000000000" => "b"); // Array key could be integer or string, it can fit into a 64-bit (but not 32-bit) integer.
Noch schlimmer ist, dass PHP dazu neigt Buggy wenn die ganze Zahl in der Nähe der 2 liegt 31 \= 2.147.483.648 Grenze (siehe Wanze 51430 , Wanze 52899 ). Zum Beispiel in meiner lokalen Umgebung (PHP 5.3.8 auf XAMPP 1.7.7 auf Windows 7), var_dump(array("2147483647" => "b"))
gibt
array(1) {
[2147483647]=>
string(1) "b"
}
sondern auf diese Live-Demo auf Codepad (PHP 5.2.5), ergibt derselbe Ausdruck
array(1) {
["2147483647"]=>
string(1) "b"
}
Der Schlüssel ist also ein Ganzzahl in einer Umgebung, sondern eine String in einem anderen, auch wenn 2147483647
ist eine gültige vorzeichenbehaftete 32-Bit Ganzzahl .
526 Stimmen
Es gibt einen Fehler in Ihrem Code: Die Tomate ist eine Frucht.
13 Stimmen
Diese Methode ist mit einigen Vorbehalten verbunden, aber ich mache oft einfach
if (isset($array[0]))
die einfach und schnell ist. Natürlich sollten Sie zunächst sicherstellen, dass das Array nicht leer ist, und Sie sollten einige Kenntnisse über den möglichen Inhalt des Arrays haben, damit die Methode nicht fehlschlägt (wie gemischt numerisch/assoziativ oder nicht-sequentiell).1 Stimmen
@OlleHärstedt Nicht laut US High Court. ;-)
1 Stimmen
@MCEmperor Wiki's eigene Seite über "Tomate" sagt, dass es eine Frucht ist de.wikipedia.org/wiki/Tomate Definitionen ändern sich drastisch, wenn Geld ins Spiel kommt :P Frag einfach Jaffa "cake" danach
16 Stimmen
PHP 8.1 führt dafür eine neue Funktion ein:
array_is_list
: stackoverflow.com/a/69859866/7082164