Wie kann man PHP-Arrays effektiv auf Ganzzahlinhalte validieren?

Susan Sarandon
Freigeben: 2024-10-17 13:53:29
Original
255 Leute haben es durchsucht

How to Validate PHP Arrays for Integer Content Effectively?

Efficient Array Validation for Integer Content

In PHP, verifying whether an array exclusively contains integers can be achieved through various approaches. While manual iteration and conditional checks are a viable option, there exists a more concise solution utilizing native PHP functionality.

Filtering with array_filter

The array_filter function offers a convenient and efficient method for this task. It filters an array based on a provided callback function, returning a new array containing only the elements that satisfy the condition. In our case, we can employ the built-in is_int function as the callback to isolate integer elements:

<code class="php">$only_integers       === array_filter($only_integers,       'is_int'); // true
$letters_and_numbers === array_filter($letters_and_numbers, 'is_int'); // false</code>
Nach dem Login kopieren

Abstraction with Higher-Order Functions

To enhance code readability and reusability, we can define helper functions that generalize our validation process. These functions leverage higher-order functions, which operate on functions as arguments, providing greater flexibility.

<code class="php">// Check if all array elements pass the predicate
function all($elems, $predicate) {
  foreach ($elems as $elem) {
    if (!call_user_func($predicate, $elem)) {
      return false;
    }
  }

  return true;
}

// Check if any array element passes the predicate
function any($elems, $predicate) {
  foreach ($elems as $elem) {
    if (call_user_func($predicate, $elem)) {
      return true;
    }
  }

  return false;
}</code>
Nach dem Login kopieren

With these helpers, our original validation becomes a concise and declarative expression:

<code class="php">all($only_integers,       'is_int'); // true
any($letters_and_numbers, 'is_int'); // false</code>
Nach dem Login kopieren

Simplified Invocation

To further simplify the validation process, we can encapsulate the helpers within a custom function that accepts an array and returns a boolean flag:

<code class="php">function array_has_only_ints($array) {
  return all($array, 'is_int');
}</code>
Nach dem Login kopieren

This function provides a con

Das obige ist der detaillierte Inhalt vonWie kann man PHP-Arrays effektiv auf Ganzzahlinhalte validieren?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!