Inleiding tot multidimensionale array in PHP

Een multidimensionale array is niets bijzonders dan een array in een andere array. Elke index van de reeks bevat een andere reeks in plaats van een enkel element dat opnieuw kan wijzen naar een andere reeks of de specifieke elementen. deze sub-arrays binnen de array worden benaderd met behulp van de meerdere dimensies beginnend bij de buitenste array en op weg naar de binnenste array. Dimensies zijn in feite de indices die nodig zijn om de waarde op een bepaalde positie in een array te openen of op te slaan. Multidimensionale arrays in php worden veel gebruikt in realtime-toepassingen, maar het is best lastig om ze te behandelen als een vergelijking met eendimensionale arrays vanwege de meerdere haakjes en enige complexiteit om ermee te werken, toegang tot of opslaan van waarden op een specifieke index, gebruik van lussen is vereist.

Syntaxis van multidimensionale array in PHP

Hieronder wordt de algemene syntaxis van multidimensionale arrays in PHP gegeven. Hoewel multidimensionale arrays in PHP 2D, 3D, 4D, enzovoort kunnen zijn. Hoe meer dimensionale array het is, hoe moeilijker het is om ze te beheren en hoe meer haakjes er voor de arraynaam worden toegevoegd.

Syntaxis voor 2D-array:

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

Syntaxis voor 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
)

Hoe multidimensionale arrays in PHP declareren?

PHP Hiermee kunnen multidimensionale arrays worden geïndexeerd of associatief. Associatieve arrays zijn interactiever in vergelijking met de geïndexeerde. PHP biedt een zeer eenvoudige manier om een ​​multidimensionale array in PHP te declareren met behulp van het trefwoord 'array'. Om een ​​array binnen een andere array te declareren, moeten we het trefwoord 'array' toevoegen en vervolgens de elementen van die array.

1. Verklaring van 2D-array in PHP

Code:

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

OF

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

De tweede methode die hierboven wordt getoond, wordt vaak gebruikt omdat deze vrij gemakkelijk te begrijpen is.

2. Verklaring van 3D-array in PHP

Code:

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

De bovenstaande verklaring is puur geïndexeerd als een van de 3D-arrays omdat er geen sleutel / waarde-paren worden gebruikt voor de koppeling.

Hoe een multidimensionale array in PHP te initialiseren?

Het initialiseren van een multidimensionale array betekent dat de waarden of elementen op de specifieke positie of indices van een array worden toegewezen. Het initialiseren van een multidimensionale array in PHP is vrij eenvoudig, zoals declareren. Het enige om in gedachten te houden is het gebruik van accolades tijdens het initialiseren van de subreeksen. Terwijl de waarden in een multidimensionale array worden geïnitialiseerd, kan de hoofdarray worden geïndexeerd of associatief, in het onderstaande voorbeeld is de hoofdarray de associatieve array met de sleutels Like Levis, Lee, Denizen, enz.,

1. Initialisatie van 2D-array in PHP

Code:

<_?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. Initialiseren van 3D-array in PHP

Initialisatie van 3D-arrays is hetzelfde als de 2D-arrays, het enige verschil tussen de twee is de afmetingen. De 3D-array vereist 1 index meer om deze te initialiseren dan een 2D-array. Het aantal dimensies van de array neemt toe, het aantal indices om het te initialiseren neemt ook toe. In het onderstaande voorbeeld is de hoofdarray een eenvoudige geïndexeerde array met subarrays zelf. We kunnen de hoofdarray in het onderstaande voorbeeld ook associatief maken, zoals we hebben gedaan in een 2D-array met de sleutel als de merknaam, waardoor de klant deze beter kan begrijpen tijdens het openen en opslaan.

Code:

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

Toegang tot multidimensionale arrays in PHP

Toegang tot multidimensionale arrays in PHP is heel eenvoudig en wordt gedaan met behulp van de voor of voor elke lus die de meest gebruikte lussen in PHP zijn. Voor de geïndexeerde arrays kan toegang tot matrixelementen normaal worden gedaan met behulp van het rij- en kolomnummer dat vergelijkbaar is met andere talen zoals C, Java, enz. (Arr (row_Num) (column_Num))

In het geval van associatieve arrays, wordt toegang tot de elementen van een multidimensionale array gedaan met behulp van de sleutel en waardeparen (sleutel => Waarde). Hoewel de elementen toegankelijk zijn via de eenvoudige voor of voor elke lus. Raadpleeg het onderstaande voorbeeld voor een duidelijk begrip van de toegang tot elementen in multidimensionale arrays.

Soorten multidimensionale array in PHP

Er is geen specifieke toestand tot welke de multidimensionale arrays in een PHP kunnen bestaan. Het hangt af van de specifieke situatie en het scenario. De afmetingen van een array variëren dienovereenkomstig. Normaal gebruiken programmeurs 2D- en 3D-arrays omdat het na 3D-arrays een beetje moeilijk is om ze te beheren.

Zoals we de verklaring, initialisatie en toegang tot multidimensionale arrays in PHP hebben begrepen, is het tijd voor een korte korte uitleg met voorbeelden.

1. 2D-array in PHP

2D-arrays zijn in feite een array binnen een andere array. Overweeg een scenario dat een gebruiker 10 boeken heeft en elk boek een andere naam, kosten, type heeft. In dit geval kan de programmeur een reeks boeknummers maken en elk element van de hoofdreeks bevat de reeks met details van het boek, zoals naam, kosten en type.

Code:



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

Boeknummer is $ row_num

";
voor ($ col_num = 0; $ col_num <3; $ col_num ++) (
// Toegang tot een bepaald element in een 2D-array
echo $ books ($ row_num) ($ col_num);
)
echo "
";
)
?>

Output:

2. 3D-array in PHP

3D-arrays zijn een uitbreiding van 2D-arrays. 3D-arrays bevatten nog een dimensie en bieden de mogelijkheid om meer gedetailleerde informatie toe te voegen. Overweeg een scenario van werknemerarray, waarin werknemer naam, bedrijf en jaar heeft en elke werknemer een bedrijfsprofiel heeft met de attributen id, vaardigheden en profiel. Elke werknemer heeft persoonlijke gegevens ook met de details van de stad, de staat en het land. Om op te slaan, zou de bovenstaande gegevens 3D-array vereist zijn.

Code:



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

Output:

In het bovenstaande voorbeeld worden de details van de werknemer samen met hun vaardigheden op een zeer gebruiksvriendelijke manier weergegeven. Hiermee kunt u elke werknemer detailleren in een fraaie 3D-reeks. We hebben te maken met 3d-arrays, om daar toegang toe te krijgen, moeten we eerst de hoofdarray bereiken en vervolgens de index die opnieuw de subarray bevat en vervolgens de elementen van de subarray. Op deze manier werkt toegang tot de elementen in het geval van multidimensionale arrays vanaf de buitenste tot de binnenste reeks. op dezelfde manier zijn er in het echte leven subarrays of gedetailleerde dingen waarin multidimensionale arrays worden gebruikt.

Conclusie

De bovenstaande uitleg laat duidelijk zien hoe de multidimensionale arrays in php worden gebruikt, samen met hun basissyntaxis en initialisatie. Multidimensionale arrays spelen een belangrijke rol als het gaat om het werken aan echte problemen, omdat de gebruiker de gegevens in een gedetailleerde vorm kan opslaan. Bovendien, zoals hierboven getoond, maakt php het mogelijk om de multidimensionale gegevens op te slaan in geïndexeerde of associatieve vorm volgens de vereisten, wat het vriendelijker maakt om toegang te krijgen tot de gegevens en deze op te slaan.

Aanbevolen artikelen

Dit is een gids voor Multidimensional Array in PHP. Hier bespreken we de declaratie, initialisatie van multidimensionale array in php met zijn typen. U kunt ook de volgende artikelen bekijken voor meer informatie -

  1. PHP Magic Constants (werken en voorbeelden)
  2. Top 13 belangrijkste kenmerken van Laravel
  3. Socket programmeren in PHP
  4. Soorten overbelasting in PHP
  5. Lussen in VBScript met voorbeelden
  6. Socket programmeren in Python