Test si un tableau est multidimensionnel

  Information

Fonction qui test si un tableau est multi-dimensionnel. Le tableau est passé en paramètre et la fonction retourne TRUE si c'est un tableau multi-dimensionnel.
Auteur : Damien Seguy (Nexen)

  code source classé dans  Tableaux - Array

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 10    
 11    
 12    
 13    
 14    
 15    
 16    
 17    
 18    
 19    
 20    
 21    
 22    
 23    
                               
<?php
/*---------------------------------------------------------------*/
/*
    Titre : Test si un tableau est multidimensionnel                                                                      
                                                                                                                          
    URL   : https://phpsources.net/code_s.php?id=1030
    Date édition     : 16 Fév 2019                                                                                        
    Date mise à jour : 02 Sept 2019                                                                                      
    Rapport de la maj:                                                                                                    
    - fonctionnement du code vérifié                                                                                    
*/
/*---------------------------------------------------------------*/

    function is_multidim_array($array){
     if (!is_array($array)){ return FALSE;} 
      foreach($array as $a){ 
      if(is_array($a)) { return TRUE;} 
      } 
      return FALSE
    }
?>

Exemple :

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 10    
 11    
 12    
 13    
 14    
 15    
 16    
 17    
 18    
 19    
 20    
 21    
 22    
 23    
 24    
                                
<?php

    $in array(
    => array('prenom' => 'Monique''nom' => 'Debby'),
    => array('prenom' => 'Marion''nom' => 'Sonia'));

    if (is_multidim_array($in))
       echo 'oui';
       else
       echo 'non';

       // Affiche : oui

    $in array('prenom' => 'Monique''nom' => 'Debby');

    if (is_multidim_array($in))
       echo 'oui';
       else
       echo 'non';

       // Affiche : non
?>

          Fonctions du code - Doc officielle PHP

   php.net   Description Versions PHP OUTIL
   array Crée un tableau PHP 4, PHP 5, PHP 7, PHP 8
   echo Affiche une chaîne de caractères PHP 4, PHP 5, PHP 7, PHP 8
   is_array Détermine si une variable est un tableau PHP 4, PHP 5, PHP 7, PHP 8
   return Retourne le controle du programme au module appelant. PHP 4, PHP 5, PHP 7, PHP 8

   Dites merci aux auteurs pour leurs travail, ça ne coûte rien et ça fait toujours plaisir wink

 Autres snippets qui pourraient vous intéressez

Affiche tous les éléments d'un tableau multidimensionnel

Compatibilité : PHP 5, PHP 7

Affiche tous les éléments récursivement d'un tableau multidimensionnel. La fonction s’appelle elle-même jusqu'au dernier élément rencontré du tableau.

Test si c'est un tableau d'entiers

Compatibilité : PHP 5, PHP 7

Similaire à la fonction is_int () mais pour les tableaux. Retourne True si tous les éléments du tableau sont de type nombre entier.

Test l'existence de doublons dans un tableau associatif

Compatibilité : PHP 5, PHP 7

Fonction qui permet de savoir si le tableau passé en argument contient des doublons. La fonction renvoie true si il y a au moins 1 doublon, sinon false.

Transforme un tableau en un tableau associatif

Compatibilité : PHP 5, PHP 7

Transforme un tableau avec des clés numériques en un tableau associatif en reprenant les valeurs dans le même ordre.

Test la validité d'un nom de domaine

Compatibilité : PHP 5, PHP 7

Test la validité d'un nom de domaine avec un REGEX, les extensions testées sont : .com, .fr, .org, .net, .info, .ca

Présentation de PHP

PHP débutant et initié 50 Tutoriel

Présentation de MySQL

  16 Fév 2019

Information sur les mises à jour

Dernière mise à jour :

    02 Sept 2019
    fonctionnement du code vérifié

2 173 Vues
Compatibilité
PHP 5, 7 et 8+