Introduktion til multidimensional Array i PHP

En flerdimensionel matrix er intet ekstraordinært end et array inde i et andet array. Hver indeks i arrayet indeholder en anden matrix i stedet for et enkelt element, der igen kan pege på et andet array eller de bestemte elementer. disse sub-arrays inde i arrayet fås ved hjælp af de flere dimensioner, der starter fra det ydre array og bevæger sig mod det indre array. Dimensioner er dybest set de indekser, der kræves for at få adgang til eller gemme værdien på en bestemt position i en matrix. Multidimensionelle arrays i php er meget brugt i realtidsapplikationer, men det er ret vanskeligt at håndtere dem som en sammenligning med enkeltdimensionelle arrays på grund af de flere parenteser og en vis kompleksitet for at arbejde med dem, enten adgang til eller lagring af værdier på en bestemt indeks, brug af sløjfer er påkrævet.

Syntaks af multidimensional Array i PHP

Nedenfor er vist den generelle syntaks af multidimensionelle arrays i PHP. Selvom multidimensionelle arrays i PHP kan være 2D, 3D, 4D og så videre. Jo mere dimensionelt array det er, jo vanskeligere er det at styre dem, og flere tilføjes parenteserne foran array-navnet.

Syntaks til 2D Array:

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

Syntaks til 3D Array:

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
)

Hvordan erklæres multidimensionelle arrays i PHP?

PHP Tillader, at dens multidimensionelle arrays enten er indekseret eller associerende. Associerende arrays er mere interaktive sammenlignet med det indekserede. PHP tillader en meget enkel måde at erklære en multidimensionel matrix i PHP ved hjælp af nøgleordet 'array'. For at erklære en matrix i en anden matrix, er vi nødt til at tilføje nøgleordet 'array' og derefter elementerne i den array.

1. Erklæring om 2D Array i PHP

Kode:

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

ELLER

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

Den anden metode, der er vist ovenfor, er almindeligt anvendt, da den er ganske let at forstå.

2. Erklæring om 3D Array i PHP

Kode:

<_?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)),
),
);
?>

Ovenstående deklaration er rent indekseret et af 3D-arrays, da der ikke er nogen nøgleværdipar, der bruges til foreningen.

Sådan initialiseres en multidimensionel matrix i PHP?

Initialisering af en flerdimensionel matrix betyder, at der tildeles værdier eller elementer i den specifikke position eller indeks for en matrix. At initialisere en flerdimensionel matrix i PHP er ganske let som at erklære. Den eneste ting at huske er brugen af ​​seler, mens du initialiserer subarrays. Mens initialiseringen af ​​værdierne i en multidimensionel matrix kan hovedgruppen indekseres eller assosierende, i eksemplet nedenfor er hovedgruppen den associative, der har tasterne Ligesom Levis, Lee, Denizen, Etc.,

1. Initialisering af 2D Array i PHP

Kode:

<_?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. Initialisering af 3D Array i PHP

Initialisering af 3D-arrays er den samme som 2D-arrays, den eneste forskel mellem de to er dimensionerne. 3D Array kræver 1 mere indeks for at initialisere den end en 2D Array. Antallet af dimensioner af array øges, antallet af indekser, der initialiseres, øges også. I nedenstående eksempel er hovedgruppen en simpel indekseret matrix med undergrupper i sig selv. Vi kan også lave hovedgruppen i nedenstående eksempel som associerende som vi har gjort i en 2D array med nøglen som mærkenavn, hvilket gør det lettere for kunden at forstå, mens han får adgang til og gemmer det.

Kode:

<_?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
)
),
);
?>

Adgang til multidimensionelle arrays i PHP

Adgang til multidimensionelle arrays i PHP er meget enkel og gøres ved at bruge enten for eller for hver løkke, som er de ofte anvendte sløjfer i PHP. For de indekserede arrays kan adgang til arrayelementer normalt udføres ved hjælp af række- og kolonnenummer svarende til andre sprog som C, Java, Etc. (arr (række_Num) (column_Num))

I tilfælde af tilknyttede arrays sker adgang til elementerne i en multidimensionel matrix ved hjælp af nøglen og værdien Pairs (key => Value). Skønt elementerne er tilgængelige gennem det enkle til eller for hver løkke. Se det nedenfor givne eksempel for en klar forståelse af adgangen til elementer i multidimensionelle arrays.

Typer af multidimensionel matrix i PHP

Der er ingen særlig tilstand, indtil de multidimensionelle arrays kan eksistere i en PHP. Det afhænger af den særlige situation og scenarie. Dimensioner på en matrix varierer i overensstemmelse hermed. Normalt bruger programmerere 2D- og 3D-arrays, fordi det efter 3D-arrays er lidt svært at styre dem.

Som vi har forstået erklæringen, initialiseringen og adgangen til multidimensionelle arrays i PHP, er det tid til en hurtig kort forklaring med eksempler.

1. 2D Array i PHP

2D-arrays er dybest set array i en anden matrix. Overvej et scenario, hvor en bruger har 10 bøger, og hver bog har et andet navn, pris, type. I dette tilfælde kan programmereren oprette en række bognumre, og hvert element i hovedgruppen indeholder arrayet, der indeholder detaljer om bogen som navn, pris og type.

Kode:



/* 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 "

Bognummer er $ række_nummer

";
for ($ col_num = 0; $ col_num <3; $ col_num ++) (
// Adgang til et bestemt element i en 2D-matrix
ekko $ bøger ($ række_nummer) ($ col_num);
)
ekko "
";
)
?>

Produktion:

2. 3D Array i PHP

3D-arrays er en udvidelse af 2D-arrays. 3D-arrays indeholder en dimension mere og giver mulighed for at tilføje mere detaljerede oplysninger. Overvej et scenario med medarbejdergruppe, hvor medarbejderen har navn, firma og år, og hver medarbejder har en virksomhedsprofil med attributterne id, færdigheder og profil. Hver medarbejder har personlige data også med oplysninger om byen, staten og landet. For at gemme skal 3D Data Array over data være påkrævet.

Kode:



$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 " ";
?>

Produktion:

Ovenstående eksempel viser tydeligt medarbejderens detaljer sammen med deres færdigheder på en meget brugervenlig måde. Det giver mulighed for detaljering af hver enkelt medarbejder i en fancy 3d matrix. Vi beskæftiger os med 3d-arrays, for at få adgang til det, er vi nødt til først at nå ud til hovedgruppen og derefter til indekset, som igen holder undergruppen og derefter til elementerne i dens undergruppe. På denne måde fungerer adgang til elementerne i tilfælde af flerdimensionelle arrays, der starter fra det yderste til det inderste array. Tilsvarende er der i det virkelige liv subarrays eller detaljerede ting, hvor multidimensionelle arrays bruges.

Konklusion

Ovenstående forklaring viser klart, hvordan de multidimensionelle arrays bruges i php sammen med deres grundlæggende syntaks og initialisering. Multidimensionelle arrays spiller en vigtig rolle, når det kommer til at arbejde på problemer i det virkelige liv, da de giver brugeren mulighed for at gemme dataene i en detaljeret form. Som vist ovenfor tillader php endvidere lagring af multidimensionelle data enten i indekseret eller assosiativ form i henhold til kravene, hvilket gør det mere venligt at få adgang til og gemme dataene.

Anbefalede artikler

Dette er en guide til multidimensional Array i PHP. Her diskuterer vi erklæringen, initialisering af multidimensionel matrix i php med dens typer. Du kan også se på de følgende artikler for at lære mere -

  1. PHP magiske konstanter (arbejde og eksempler)
  2. Top 13 nøglefunktioner i Laravel
  3. Socket-programmering i PHP
  4. Typer af overbelastning i PHP
  5. Loops i VBScript med eksempler
  6. Socket-programmering i Python

Kategori: