Sissejuhatus mitmemõõtmelisse massiivi PHP-s

Mitmemõõtmeline massiiv pole midagi erakordset, kui massiiv teise massiivi sees. Massiivi igas indeksis on ühe elemendi asemel teine ​​massiiv, mis võib jälle osutada teisele massiivile või konkreetsetele elementidele. neile massiivi sisemistele alammassiividele pääseb juurde mitme mõõtmega, alustades välimisest massiivist ja liikudes sisemise massiivi poole. Mõõtmed on põhimõtteliselt indeksid, mida on vaja väärtuse juurde pääsemiseks või talletamiseks massiivi konkreetses asukohas. PHP-s sisalduvaid mitmemõõtmelisi massiive kasutatakse reaalajas rakendustena laialdaselt, kuid nende võrdlemiseks ühemõõtmeliste massiividega on üsna keeruline, kuna neil on mitu sulgu ja keerukus, et nendega koos töötada, väärtustele juurde pääseda või neid salvestada. konkreetne indeks, silmuste kasutamine on vajalik.

Mitmemõõtmelise massiivi süntaks PHP-s

Allpool on toodud PHP mitmemõõtmeliste massiivide üldine süntaks. Ehkki PHP-s võivad mitmemõõtmelised massiivid olla 2D, 3D, 4D jne. Mida mõõtmelisem massiiv on, seda keerulisem on neid hallata ja massiivinime ette on lisatud sulud.

2D-massiivi süntaks:

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

3D-massiivi süntaks:

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
)

Kuidas kuulutada mitmemõõtmelisi massiive PHP-s?

PHP võimaldab mitmemõõtmelisi massiive kas indekseerida või seostada. Assotsiatiivsed massiivid on indekseeritutega võrreldes interaktiivsemad. PHP võimaldab väga lihtsat viisi mitmemõõtmelise massiivi deklareerimiseks PHP-s, kasutades märksõna 'massiiv'. Mõnes teises massiivis massiivi kuulutamiseks peame lisama märksõna 'massiiv' ja seejärel selle massiivi elemendid.

1. 2D-massiivi deklareerimine PHP-s

Kood:

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

VÕI

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

Ülaltoodud teist meetodit kasutatakse tavaliselt, kuna seda on üsna lihtne mõista.

2. 3D-massiivi deklaratsioon PHP-s

Kood:

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

Ülaltoodud deklaratsioon on puhtalt indekseeritud üks 3D-massiividest, kuna seostamiseks pole kasutatud võtme-väärtuse paare.

Kuidas PHP-s mitmemõõtmelist massiivi initsialiseerida?

Mitmemõõtmelise massiivi initsialiseerimine tähendab väärtuste või elementide määramist massiivi konkreetses asukohas või indeksites. Mitmemõõtmelise massiivi initsialiseerimine PHP-s on üsna lihtne nagu deklareerimine. Ainus asi, mida tuleks meeles pidada, on alamkihtide initsialiseerimisel breketite kasutamine. Väärtuste initsialiseerimisel mitmemõõtmelises massiivis võib põhimassiiv olla indekseeritud või assotsiatiivne, allpool toodud näites on põhimassiiv assotsiatiivne, millel on võtmed nagu Levis, Lee, Denizen, jne,

1. 2D-massiivi lähtestamine PHP-s

Kood:

<_?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. 3D massiivi lähtestamine PHP-s

3D-massiivide lähtestamine on sama mis 2D-massiividega, ainus erinevus nende vahel on mõõtmed. 3D-massiiv vajab selle initsialiseerimiseks veel ühte indeksit kui 2D-massiiv. Massiivi mõõtmete arv suureneb, suureneb ka selle initsialiseerimiseks kasutatavate indeksite arv. Allolevas näites on põhimassiiv lihtne indekseeritud massiiv, millel on alammassiive. Samuti võime teha alljärgnevas näites põhimassiivi assotsiatiivseks, nagu oleme teinud kahesuunalises massiivis võtmega kui brändinimega, mis lihtsustab kliendi mõistmist sellele juurde pääsedes ja talletades.

Kood:

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

Ligipääs mitmemõõtmelistele massiividele PHP-s

Mitmemõõtmelistele massiividele juurdepääs PHP-s on väga lihtne ja seda kasutatakse kas või iga silmuse jaoks, mis on PHP-s tavaliselt kasutatavad silmused. Indekseeritud massiivide jaoks saab massiivi elementidele juurdepääsu tavaliselt teha, kasutades rea ja veeru numbrit, mis sarnaneb muude keeltega, nagu C, Java, jne (arr (rea_Num) (veeru_ number))

Assotsiatiivsete massiivide korral toimub mitmemõõtmelise massiivi elementide juurde pääsemine võtme ja väärtusepaaride abil (võti => väärtus). Elementide juurde pääseb lihtsa või iga silmuse kaudu. Palun lugege allpool toodud näidet, et saada mitmemõõtmelistes massiivides elementide juurde pääsemise selge ülevaade.

Mitmemõõtmelise massiivi tüübid PHP-s

Pole ühtegi konkreetset olekut, milleni mitmemõõtmelised massiivid võiksid PHP-s eksisteerida. See sõltub konkreetsest olukorrast ja stsenaariumist. Massiivi mõõtmed varieeruvad vastavalt. Tavaliselt kasutavad programmeerijad 2D- ja 3D-massiive, sest pärast 3D-massiive on neid natuke keeruline hallata.

Nagu oleme aru saanud mitmemõõtmeliste massiivide deklareerimisest, lähtestamisest ja kasutamisest PHP-s, on aeg kiireks lühikeseks selgituseks näidetega.

1. 2D-massiiv PHP-s

2D-massiivid on põhimõtteliselt massiivid teise massiivi sees. Mõelge stsenaariumile, kus kasutajal on 10 raamatut ja igal raamatul on erinev nimi, maksumus, tüüp. Sel juhul saab programmeerija luua raamatunumbrite massiivi ja iga põhimassiivi element hoiab massiivi, mis sisaldab raamatu üksikasju, nagu nimi, maksumus ja tüüp.

Kood:



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

Raamatu number on $ row_num

";
jaoks ($ col_num = 0; $ col_num <3; $ col_num ++) (
// Juurdepääs kindlale elemendile 2D massiivis
echo $ raamatud ($ row_num) ($ col_num);
)
kaja "
";
)
?>

Väljund:

2. 3D massiiv PHP-s

3D-massiivid on 2D-massiivide laiendus. 3D-massiivid sisaldavad veel ühte mõõdet ja annavad võimaluse täiendava teabe lisamiseks. Mõelge töötajate massiivi stsenaariumile, kus töötajal on nimi, ettevõte ja aasta ning igal töötajal on ettevõtte profiil, millel on atribuudid id, oskused ja profiil. Igal töötajal on isikuandmed koos linna, osariigi ja riigi andmetega. Selleks, et säilitada, oleks vaja ülaltoodud andmete 3D massiivi.

Kood:



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

Väljund:

Ülaltoodud näites on selgelt välja toodud töötaja andmed ja oskused väga kasutajasõbralikul viisil. See võimaldab üksikasjalikult kirjeldada kõiki töötajaid väljamõeldud 3D-massiivides. Tegemist on 3D-massiividega, et sellele juurde pääseda, peame esmalt jõudma põhimassiivini ja seejärel indeksini, mis jällegi hoiab alammassiivi, ja seejärel selle alammassiivi elementide juurde. Sel viisil toimib juurdepääs elementidele mitmemõõtmeliste massiivide korral, alustades kõige välimisest kuni sisemiseni massiivini. samamoodi on reaalses elus alammassiive või üksikasjalikke asju, milles kasutatakse mitmemõõtmelisi massiive.

Järeldus

Ülaltoodud selgitus näitab selgelt, kuidas mitmemõõtmelisi massiive kasutatakse php-s koos nende põhisüntaksi ja initsiatsiooniga. Mitmemõõtmelised massiivid mängivad olulist rolli reaalse elu probleemidega tegelemisel, kuna need võimaldavad kasutajal andmeid üksikasjalikult säilitada. Veelgi enam, nagu ülal näidatud, võimaldab php mitmemõõtmeliste andmete salvestamist vastavalt vajadusele kas indekseeritud või assotsieerunud kujul, mis muudab andmetele juurdepääsu ja salvestamise sõbralikumaks.

Soovitatavad artiklid

See on juhend mitmemõõtmelise massiivi kohta PHP-s. Siin käsitleme deklaratsiooni, mitmemõõtmelise massiivi initsialiseerimist php-s koos selle tüüpidega. Lisateabe saamiseks võite vaadata ka järgmisi artikleid -

  1. PHP võlukonstandid (töö ja näited)
  2. Laraveli 13 parimat põhijoont
  3. Pistikupesa programmeerimine PHP-s
  4. Ülekoormamise tüübid PHP-s
  5. Silmused VBScriptis näidetega
  6. Socketi programmeerimine Pythonis

Kategooria: