Introduzione alla matrice multidimensionale in PHP

Un array multidimensionale non è altro che un array all'interno di un altro array. Ogni indice dell'array contiene un altro array anziché un singolo elemento che può nuovamente puntare a un altro array o agli elementi particolari. è possibile accedere a questi sotto-array all'interno dell'array utilizzando le dimensioni multiple a partire dall'array esterno e spostandosi verso l'array interno. Le dimensioni sono fondamentalmente gli indici necessari per accedere o memorizzare il valore in una determinata posizione in un array. Le matrici multidimensionali in php sono molto utilizzate nelle applicazioni in tempo reale, ma è piuttosto complicato gestirle come un confronto con le matrici monodimensionali a causa delle parentesi multiple e della complessità al fine di lavorare con esse, accedendo o archiviando valori in un indice particolare, è richiesto l'uso di loop.

Sintassi dell'array multidimensionale in PHP

Di seguito è riportata la sintassi generale degli array multidimensionali in PHP. Sebbene le matrici multidimensionali in PHP possano essere 2D, 3D, 4D e così via. Più array dimensionale è, più è difficile gestirli e più sono le parentesi aggiunte davanti al nome dell'array.

Sintassi per array 2D:

array(
array(element1, element2, elements3, …),
array(element1, element2, elements3, …),
… so on
)

Sintassi per array 3D:

array(
array (
array(element1, element2, elements3, …),
array(element1, element2, elements3, …),
… so on
),
array (
array(element1, element2, elements3, …),
array(element1, element2, elements3, …),
… so on
),
… so on
)

Come dichiarare le matrici multidimensionali in PHP?

PHP consente alle sue matrici multidimensionali di essere indicizzate o associative. Le matrici associative sono più interattive rispetto a quelle indicizzate. PHP consente un modo molto semplice per dichiarare un array multidimensionale in PHP usando la parola chiave 'array'. Per dichiarare un array all'interno di un altro array, è necessario aggiungere la parola chiave 'array' e quindi gli elementi di tale array.

1. Dichiarazione di array 2D in PHP

Codice:

<_?php
$employee_details = array();
$employee_details( ) = array(“Ram”, “Agra”, “Sr. Engineer”);
$employee_details( ) = array(“Raghav”, “Delhi”, “Jr. Engineer”);
?>

O

<_?php
$employee_details = array(
array(“Ram”, “Agra”, “Sr. Engineer”),
array(“Raghav”, “Delhi”, “Jr. Engineer”),
);
?>

Il secondo metodo mostrato sopra è comunemente usato in quanto è abbastanza facile da capire.

2. Dichiarazione di array 3D in PHP

Codice:

<_?php
/* Simplest way to declare a 3D array in Php in an indexed manner */
$item_details = array(
array(
array (“item1”, “abc”, 100)),
array (“item2”, “bcd”, 200)),
array (“item3”, “def”, 300)),
),
array(
array (“item4”, “abc4”, 100)),
array (“item5, “bcd5”, 200)),
array (“item6”, “def6”, 300)),
),
);
?>

La dichiarazione di cui sopra è puramente indicizzata in una matrice 3D in quanto non esistono coppie chiave-valore utilizzate per l'associazione.

Come inizializzare un array multidimensionale in PHP?

Inizializzare un array multidimensionale significa assegnare i valori o gli elementi nella posizione o negli indici particolari di un array. Inizializzare un array multidimensionale in PHP è abbastanza facile come dichiarare. L'unica cosa da tenere a mente è l'uso di parentesi graffe durante l'inizializzazione dei sottoparagrafi. Durante l'inizializzazione dei valori in un array multidimensionale, l'array principale può essere indicizzato o associativo, nell'esempio riportato di seguito, l'array principale è quello associativo con le chiavi come Levis, Lee, Denizen, Etc.,

1. Inizializzazione di array 2D in PHP

Codice:

<_?php
/* It is a multidimensional 2D array of clothes in which the main array holds another arrays of having 2 elements like cloth type and quantity */
/* It is associative kind of array having the data in the form of key => value pairs. So the data at the inner subarray is represented as associated by the key element.*/
$clothes = array(
“Levis” => array(
“Cloth_type” => “jeans”,
“Quantity” => 20
),
“Pepe” => array(
“Cloth_type” => “jeans”,
“Quantity” => 100
),
“Lee” => array(
“Cloth_type” => “tshirts”,
“Quantity” => 50
),
“Denizen” => array(
“Cloth_type” => “tops”,
“Quantity” => 80
)
);
?>

2. Inizializzazione di array 3D in PHP

L'inizializzazione delle matrici 3D è la stessa delle matrici 2D, l'unica differenza tra le due è le dimensioni. L'array 3D richiede 1 indice in più per inizializzarlo rispetto a un array 2D. Aumenta il numero di dimensioni dell'array, aumenta anche il numero di indici per inizializzarlo. Nell'esempio seguente, l'array principale è un array indicizzato semplice con sotto-array in sé. Nell'esempio che segue possiamo anche rendere l'array principale come associativo come abbiamo fatto in un array 2D con la chiave come nome commerciale che rende più semplice la comprensione da parte del cliente durante l'accesso e l'archiviazione.

Codice:

<_?php
/* In this there is a 3D array of clothes in which each element have an array of cloth type, brand and quantity of that particular brand. Each brand has different quantity and cloth type.*/
$clothes = array(
array(
array(
“Brand” => “Levis”,
“Cloth_type” => “jeans”,
“Quantity” => 20
),
array(
“Brand” => “Levis”,
“Cloth_type” => “Tops”,
“Quantity” => 100
)
),
array(
array(
“Brand” => “Lee”,
“Cloth_type” => “jeans”,
“Quantity” => 50
),
array(
“Brand” => “Lee”,
“Cloth_type” => “tops”,
“Quantity” => 80
)
),
);
?>

Accesso ad array multidimensionali in PHP

L'accesso alle matrici multidimensionali in PHP è molto semplice e viene fatto utilizzando il ciclo for o per ogni loop che è il loop comunemente usato in PHP. Per gli array indicizzati, l'accesso agli elementi dell'array può essere eseguito normalmente utilizzando il numero di riga e colonna simile ad altre lingue come C, Java, ecc. (Arr (row_Num) (column_Num))

Nel caso di array associativi, l'accesso agli elementi di un array multidimensionale viene effettuato utilizzando la coppia chiave e valore (chiave => Valore). Sebbene gli elementi siano accessibili tramite il semplice per o per ogni ciclo. Fare riferimento all'esempio riportato di seguito per una chiara comprensione dell'accesso agli elementi negli array multidimensionali.

Tipi di array multidimensionali in PHP

Non esiste uno stato particolare fino al quale le matrici multidimensionali possono esistere in un PHP. Dipende dalla situazione e dallo scenario particolari. Le dimensioni di un array variano di conseguenza. Normalmente i programmatori usano array 2D e 3D perché, dopo gli array 3D, è un po 'difficile gestirli.

Come abbiamo compreso la dichiarazione, l'inizializzazione e l'accesso agli array multidimensionali in PHP, è tempo di una breve spiegazione breve con esempi.

1. Matrice 2D in PHP

Gli array 2D sono sostanzialmente array all'interno di un altro array. Considera uno scenario in cui un utente ha 10 libri e ogni libro ha un nome, un costo, un tipo diversi. In questo caso, il programmatore può creare una matrice di numeri di libri e ogni elemento della matrice principale contiene la matrice che contiene i dettagli del libro come nome, costo e tipo.

Codice:



/* Multidimensional 2D array for 4 books and each book having a different array containing book name, cost and type. */
$books = array(
array("Fiction ", "Action and Adventure ", 800),
array("Fiction ", "Anthology ", 1000),
array("Non- Fiction ", "Biography ", 600),
array("Non- Fiction ", "Cook Book ", 900)
);
/* Accessing of a 2D array with the row_number and column_number */
for ($row_num = 0; $row_num < 4; $row_num++) (
echo "
<_?php


/* Multidimensional 2D array for 4 books and each book having a different array containing book name, cost and type. */
$books = array(
array("Fiction ", "Action and Adventure ", 800),
array("Fiction ", "Anthology ", 1000),
array("Non- Fiction ", "Biography ", 600),
array("Non- Fiction ", "Cook Book ", 900)
);
/* Accessing of a 2D array with the row_number and column_number */
for ($row_num = 0; $row_num < 4; $row_num++) (
echo "

Il numero di libro è $ row_num

";
per ($ col_num = 0; $ col_num <3; $ col_num ++) (
// Accesso a un elemento particolare in un array 2D
echo $ books ($ row_num) ($ col_num);
)
eco "
";
)
?>

Produzione:

2. Matrice 3D in PHP

Le matrici 3D sono un'estensione delle matrici 2D. Le matrici 3D contengono un'altra dimensione e offrono la possibilità di aggiungere informazioni più dettagliate. Si consideri uno scenario di array di dipendenti, in cui i dipendenti hanno nome, azienda e anno e ogni dipendente ha un profilo aziendale con gli attributi id, competenze e profilo. Ogni dipendente ha dati personali anche con i dettagli della città, dello stato e del paese. Per poter archiviare, sarebbe necessario l'array 3D sopra i dati.

Codice:



$Employee = array(array(array("name", "company", "year"),
array("id", "skills", "profile"),
array("city", "state", "country")
),
/* array to store the name, company and year of employee*/
array(array("jiya", "Infosys", 2016),
array("ram", "ola", 2017)
),
/* array to store the id, skills and profile of employees */
array(array("E101", "PHP", "developer"),
array("E103", "mysql", "DBA")
),
/* array to store the city, state and country of employees */
array(array("Bangalore", "Karnataka", "India"),
array("San Francisco", "California", "USA")
)
);
?>
echo " ";
for ( $outermost = 0; $outermost < 3; $outermost++ )
(
echo " The outermost number $outermost";
echo " ";
for ( $row_num = 0; $row_num < 2; $row_num++ )
(
echo " Now displaying the row number $row_num";
echo " ";
for ( $col_num = 0; $col_num < 3; $col_num++ )
(
// accessing the array elements in a 3D array
echo " ".$Employee($outermost)($row_num)($col_num)." ";
)
echo " ";
echo " ";
)
echo " ";
echo " ";
)
echo " ";
?>
<_?php


$Employee = array(array(array("name", "company", "year"),
array("id", "skills", "profile"),
array("city", "state", "country")
),
/* array to store the name, company and year of employee*/
array(array("jiya", "Infosys", 2016),
array("ram", "ola", 2017)
),
/* array to store the id, skills and profile of employees */
array(array("E101", "PHP", "developer"),
array("E103", "mysql", "DBA")
),
/* array to store the city, state and country of employees */
array(array("Bangalore", "Karnataka", "India"),
array("San Francisco", "California", "USA")
)
);
?>
echo " ";
for ( $outermost = 0; $outermost < 3; $outermost++ )
(
echo " The outermost number $outermost";
echo " ";
for ( $row_num = 0; $row_num < 2; $row_num++ )
(
echo " Now displaying the row number $row_num";
echo " ";
for ( $col_num = 0; $col_num < 3; $col_num++ )
(
// accessing the array elements in a 3D array
echo " ".$Employee($outermost)($row_num)($col_num)." ";
)
echo " ";
echo " ";
)
echo " ";
echo " ";
)
echo " ";
?>
<_?php


$Employee = array(array(array("name", "company", "year"),
array("id", "skills", "profile"),
array("city", "state", "country")
),
/* array to store the name, company and year of employee*/
array(array("jiya", "Infosys", 2016),
array("ram", "ola", 2017)
),
/* array to store the id, skills and profile of employees */
array(array("E101", "PHP", "developer"),
array("E103", "mysql", "DBA")
),
/* array to store the city, state and country of employees */
array(array("Bangalore", "Karnataka", "India"),
array("San Francisco", "California", "USA")
)
);
?>
echo " ";
for ( $outermost = 0; $outermost < 3; $outermost++ )
(
echo " The outermost number $outermost";
echo " ";
for ( $row_num = 0; $row_num < 2; $row_num++ )
(
echo " Now displaying the row number $row_num";
echo " ";
for ( $col_num = 0; $col_num < 3; $col_num++ )
(
// accessing the array elements in a 3D array
echo " ".$Employee($outermost)($row_num)($col_num)." ";
)
echo " ";
echo " ";
)
echo " ";
echo " ";
)
echo " ";
?>



    $Employee = array(array(array("name", "company", "year"),
    array("id", "skills", "profile"),
    array("city", "state", "country")
    ),
    /* array to store the name, company and year of employee*/
    array(array("jiya", "Infosys", 2016),
    array("ram", "ola", 2017)
    ),
    /* array to store the id, skills and profile of employees */
    array(array("E101", "PHP", "developer"),
    array("E103", "mysql", "DBA")
    ),
    /* array to store the city, state and country of employees */
    array(array("Bangalore", "Karnataka", "India"),
    array("San Francisco", "California", "USA")
    )
    );
    ?>
    echo " ";
    for ( $outermost = 0; $outermost < 3; $outermost++ )
    (
    echo " The outermost number $outermost";
    echo " ";
    for ( $row_num = 0; $row_num < 2; $row_num++ )
    (
    echo " Now displaying the row number $row_num";
    echo " ";
    for ( $col_num = 0; $col_num < 3; $col_num++ )
    (
    // accessing the array elements in a 3D array
    echo " ".$Employee($outermost)($row_num)($col_num)." ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    ?>


  • $Employee = array(array(array("name", "company", "year"),
    array("id", "skills", "profile"),
    array("city", "state", "country")
    ),
    /* array to store the name, company and year of employee*/
    array(array("jiya", "Infosys", 2016),
    array("ram", "ola", 2017)
    ),
    /* array to store the id, skills and profile of employees */
    array(array("E101", "PHP", "developer"),
    array("E103", "mysql", "DBA")
    ),
    /* array to store the city, state and country of employees */
    array(array("Bangalore", "Karnataka", "India"),
    array("San Francisco", "California", "USA")
    )
    );
    ?>
    echo " ";
    for ( $outermost = 0; $outermost < 3; $outermost++ )
    (
    echo " The outermost number $outermost";
    echo " ";
    for ( $row_num = 0; $row_num < 2; $row_num++ )
    (
    echo " Now displaying the row number $row_num";
    echo " ";
    for ( $col_num = 0; $col_num < 3; $col_num++ )
    (
    // accessing the array elements in a 3D array
    echo " ".$Employee($outermost)($row_num)($col_num)." ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    ?>


      $Employee = array(array(array("name", "company", "year"),
      array("id", "skills", "profile"),
      array("city", "state", "country")
      ),
      /* array to store the name, company and year of employee*/
      array(array("jiya", "Infosys", 2016),
      array("ram", "ola", 2017)
      ),
      /* array to store the id, skills and profile of employees */
      array(array("E101", "PHP", "developer"),
      array("E103", "mysql", "DBA")
      ),
      /* array to store the city, state and country of employees */
      array(array("Bangalore", "Karnataka", "India"),
      array("San Francisco", "California", "USA")
      )
      );
      ?>
      echo " ";
      for ( $outermost = 0; $outermost < 3; $outermost++ )
      (
      echo " The outermost number $outermost";
      echo " ";
      for ( $row_num = 0; $row_num < 2; $row_num++ )
      (
      echo " Now displaying the row number $row_num";
      echo " ";
      for ( $col_num = 0; $col_num < 3; $col_num++ )
      (
      // accessing the array elements in a 3D array
      echo " ".$Employee($outermost)($row_num)($col_num)." ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      ?>


    • $Employee = array(array(array("name", "company", "year"),
      array("id", "skills", "profile"),
      array("city", "state", "country")
      ),
      /* array to store the name, company and year of employee*/
      array(array("jiya", "Infosys", 2016),
      array("ram", "ola", 2017)
      ),
      /* array to store the id, skills and profile of employees */
      array(array("E101", "PHP", "developer"),
      array("E103", "mysql", "DBA")
      ),
      /* array to store the city, state and country of employees */
      array(array("Bangalore", "Karnataka", "India"),
      array("San Francisco", "California", "USA")
      )
      );
      ?>
      echo " ";
      for ( $outermost = 0; $outermost < 3; $outermost++ )
      (
      echo " The outermost number $outermost";
      echo " ";
      for ( $row_num = 0; $row_num < 2; $row_num++ )
      (
      echo " Now displaying the row number $row_num";
      echo " ";
      for ( $col_num = 0; $col_num < 3; $col_num++ )
      (
      // accessing the array elements in a 3D array
      echo " ".$Employee($outermost)($row_num)($col_num)." ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      ?>


        $Employee = array(array(array("name", "company", "year"),
        array("id", "skills", "profile"),
        array("city", "state", "country")
        ),
        /* array to store the name, company and year of employee*/
        array(array("jiya", "Infosys", 2016),
        array("ram", "ola", 2017)
        ),
        /* array to store the id, skills and profile of employees */
        array(array("E101", "PHP", "developer"),
        array("E103", "mysql", "DBA")
        ),
        /* array to store the city, state and country of employees */
        array(array("Bangalore", "Karnataka", "India"),
        array("San Francisco", "California", "USA")
        )
        );
        ?>
        echo " ";
        for ( $outermost = 0; $outermost < 3; $outermost++ )
        (
        echo " The outermost number $outermost";
        echo " ";
        for ( $row_num = 0; $row_num < 2; $row_num++ )
        (
        echo " Now displaying the row number $row_num";
        echo " ";
        for ( $col_num = 0; $col_num < 3; $col_num++ )
        (
        // accessing the array elements in a 3D array
        echo " ".$Employee($outermost)($row_num)($col_num)." ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        ?>


      • $Employee = array(array(array("name", "company", "year"),
        array("id", "skills", "profile"),
        array("city", "state", "country")
        ),
        /* array to store the name, company and year of employee*/
        array(array("jiya", "Infosys", 2016),
        array("ram", "ola", 2017)
        ),
        /* array to store the id, skills and profile of employees */
        array(array("E101", "PHP", "developer"),
        array("E103", "mysql", "DBA")
        ),
        /* array to store the city, state and country of employees */
        array(array("Bangalore", "Karnataka", "India"),
        array("San Francisco", "California", "USA")
        )
        );
        ?>
        echo " ";
        for ( $outermost = 0; $outermost < 3; $outermost++ )
        (
        echo " The outermost number $outermost";
        echo " ";
        for ( $row_num = 0; $row_num < 2; $row_num++ )
        (
        echo " Now displaying the row number $row_num";
        echo " ";
        for ( $col_num = 0; $col_num < 3; $col_num++ )
        (
        // accessing the array elements in a 3D array
        echo " ".$Employee($outermost)($row_num)($col_num)." ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        ?>


      • $Employee = array(array(array("name", "company", "year"),
        array("id", "skills", "profile"),
        array("city", "state", "country")
        ),
        /* array to store the name, company and year of employee*/
        array(array("jiya", "Infosys", 2016),
        array("ram", "ola", 2017)
        ),
        /* array to store the id, skills and profile of employees */
        array(array("E101", "PHP", "developer"),
        array("E103", "mysql", "DBA")
        ),
        /* array to store the city, state and country of employees */
        array(array("Bangalore", "Karnataka", "India"),
        array("San Francisco", "California", "USA")
        )
        );
        ?>
        echo " ";
        for ( $outermost = 0; $outermost < 3; $outermost++ )
        (
        echo " The outermost number $outermost";
        echo " ";
        for ( $row_num = 0; $row_num < 2; $row_num++ )
        (
        echo " Now displaying the row number $row_num";
        echo " ";
        for ( $col_num = 0; $col_num < 3; $col_num++ )
        (
        // accessing the array elements in a 3D array
        echo " ".$Employee($outermost)($row_num)($col_num)." ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        echo " ";
        )
        echo " ";
        ?>


      $Employee = array(array(array("name", "company", "year"),
      array("id", "skills", "profile"),
      array("city", "state", "country")
      ),
      /* array to store the name, company and year of employee*/
      array(array("jiya", "Infosys", 2016),
      array("ram", "ola", 2017)
      ),
      /* array to store the id, skills and profile of employees */
      array(array("E101", "PHP", "developer"),
      array("E103", "mysql", "DBA")
      ),
      /* array to store the city, state and country of employees */
      array(array("Bangalore", "Karnataka", "India"),
      array("San Francisco", "California", "USA")
      )
      );
      ?>
      echo " ";
      for ( $outermost = 0; $outermost < 3; $outermost++ )
      (
      echo " The outermost number $outermost";
      echo " ";
      for ( $row_num = 0; $row_num < 2; $row_num++ )
      (
      echo " Now displaying the row number $row_num";
      echo " ";
      for ( $col_num = 0; $col_num < 3; $col_num++ )
      (
      // accessing the array elements in a 3D array
      echo " ".$Employee($outermost)($row_num)($col_num)." ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      ?>


    • $Employee = array(array(array("name", "company", "year"),
      array("id", "skills", "profile"),
      array("city", "state", "country")
      ),
      /* array to store the name, company and year of employee*/
      array(array("jiya", "Infosys", 2016),
      array("ram", "ola", 2017)
      ),
      /* array to store the id, skills and profile of employees */
      array(array("E101", "PHP", "developer"),
      array("E103", "mysql", "DBA")
      ),
      /* array to store the city, state and country of employees */
      array(array("Bangalore", "Karnataka", "India"),
      array("San Francisco", "California", "USA")
      )
      );
      ?>
      echo " ";
      for ( $outermost = 0; $outermost < 3; $outermost++ )
      (
      echo " The outermost number $outermost";
      echo " ";
      for ( $row_num = 0; $row_num < 2; $row_num++ )
      (
      echo " Now displaying the row number $row_num";
      echo " ";
      for ( $col_num = 0; $col_num < 3; $col_num++ )
      (
      // accessing the array elements in a 3D array
      echo " ".$Employee($outermost)($row_num)($col_num)." ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      echo " ";
      )
      echo " ";
      ?>


    $Employee = array(array(array("name", "company", "year"),
    array("id", "skills", "profile"),
    array("city", "state", "country")
    ),
    /* array to store the name, company and year of employee*/
    array(array("jiya", "Infosys", 2016),
    array("ram", "ola", 2017)
    ),
    /* array to store the id, skills and profile of employees */
    array(array("E101", "PHP", "developer"),
    array("E103", "mysql", "DBA")
    ),
    /* array to store the city, state and country of employees */
    array(array("Bangalore", "Karnataka", "India"),
    array("San Francisco", "California", "USA")
    )
    );
    ?>
    echo " ";
    for ( $outermost = 0; $outermost < 3; $outermost++ )
    (
    echo " The outermost number $outermost";
    echo " ";
    for ( $row_num = 0; $row_num < 2; $row_num++ )
    (
    echo " Now displaying the row number $row_num";
    echo " ";
    for ( $col_num = 0; $col_num < 3; $col_num++ )
    (
    // accessing the array elements in a 3D array
    echo " ".$Employee($outermost)($row_num)($col_num)." ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    ?>


  • $Employee = array(array(array("name", "company", "year"),
    array("id", "skills", "profile"),
    array("city", "state", "country")
    ),
    /* array to store the name, company and year of employee*/
    array(array("jiya", "Infosys", 2016),
    array("ram", "ola", 2017)
    ),
    /* array to store the id, skills and profile of employees */
    array(array("E101", "PHP", "developer"),
    array("E103", "mysql", "DBA")
    ),
    /* array to store the city, state and country of employees */
    array(array("Bangalore", "Karnataka", "India"),
    array("San Francisco", "California", "USA")
    )
    );
    ?>
    echo " ";
    for ( $outermost = 0; $outermost < 3; $outermost++ )
    (
    echo " The outermost number $outermost";
    echo " ";
    for ( $row_num = 0; $row_num < 2; $row_num++ )
    (
    echo " Now displaying the row number $row_num";
    echo " ";
    for ( $col_num = 0; $col_num < 3; $col_num++ )
    (
    // accessing the array elements in a 3D array
    echo " ".$Employee($outermost)($row_num)($col_num)." ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    echo " ";
    )
    echo " ";
    ?>


$Employee = array(array(array("name", "company", "year"),
array("id", "skills", "profile"),
array("city", "state", "country")
),
/* array to store the name, company and year of employee*/
array(array("jiya", "Infosys", 2016),
array("ram", "ola", 2017)
),
/* array to store the id, skills and profile of employees */
array(array("E101", "PHP", "developer"),
array("E103", "mysql", "DBA")
),
/* array to store the city, state and country of employees */
array(array("Bangalore", "Karnataka", "India"),
array("San Francisco", "California", "USA")
)
);
?>
echo " ";
for ( $outermost = 0; $outermost < 3; $outermost++ )
(
echo " The outermost number $outermost";
echo " ";
for ( $row_num = 0; $row_num < 2; $row_num++ )
(
echo " Now displaying the row number $row_num";
echo " ";
for ( $col_num = 0; $col_num < 3; $col_num++ )
(
// accessing the array elements in a 3D array
echo " ".$Employee($outermost)($row_num)($col_num)." ";
)
echo " ";
echo " ";
)
echo " ";
echo " ";
)
echo " ";
?>

Produzione:

L'esempio sopra mostra chiaramente i dettagli dell'impiegato e le loro competenze in un modo molto user-friendly. Permette il dettaglio di ogni singolo dipendente in fantasiose matrici 3d. Abbiamo a che fare con array 3d, per accedervi dobbiamo prima raggiungere l'array principale e poi l'indice che contiene ancora il subarray e quindi gli elementi del suo subarray. In questo modo, l'accesso agli elementi funziona nel caso di array multidimensionali a partire dall'array più esterno a quello più interno. allo stesso modo, nella vita reale, ci sono sotto-matrici o cose dettagliate in cui vengono usate matrici multidimensionali.

Conclusione

La spiegazione sopra mostra chiaramente come gli array multidimensionali sono usati in php insieme alla loro sintassi di base e inizializzazione. Le matrici multidimensionali svolgono un ruolo importante quando si tratta di lavorare su problemi della vita reale in quanto consentono all'utente di memorizzare i dati in una forma dettagliata. Inoltre, come mostrato sopra, php consente di archiviare i dati multidimensionali in forma indicizzata o associativa in base ai requisiti, il che rende più semplice l'accesso e la memorizzazione dei dati.

Articoli consigliati

Questa è una guida all'array multidimensionale in PHP. Qui discutiamo la dichiarazione, l'inizializzazione di array multidimensionali in php con i suoi tipi. Puoi anche consultare i seguenti articoli per saperne di più -

  1. Costanti magiche di PHP (funzionamento ed esempi)
  2. Le 13 principali caratteristiche di Laravel
  3. Programmazione socket in PHP
  4. Tipi di sovraccarico in PHP
  5. Loop in VBScript con esempi
  6. Programmazione socket in Python

Categoria: