Следите за новинками
в нашем Telegram канале. Жми, чтобы подписаться:)
⊗ppPmMdIK 138 of 447 menu
◀ ▶

Working with Keys in Multidimensional Arrays in PHP

Working with keys when iterating over multidimensional arrays is done in the same way as when iterating over one-dimensional arrays. Let's take for example the following array:

<?php $arr = [ 'user1' => [ 'name' => 'name1', 'age' => 31, ], 'user2' => [ 'name' => 'name2', 'age' => 32, ], ]; ?>

Let's iterate over it with nested loops and output its elements with keys in the format key key element:

<?php foreach ($arr as $key1 => $sub) { foreach ($sub as $key2 => $elem) { echo $key1 . ' ' . $key2 . ' ' . $elem . '<br>'; } } ?>

Given the following array:

<?php $arr = [ [ 'name' => 'user1', 'age' => 30, 'salary' => 1000, ], [ 'name' => 'user2', 'age' => 31, 'salary' => 2000, ], [ 'name' => 'user3', 'age' => 32, 'salary' => 3000, ], ]; ?>

Output the elements of this array in the key-value format.

Given the following array:

<?php $arr = [ 'group1' => ['user11', 'user12', 'user13', 'user43'], 'group2' => ['user21', 'user22', 'user23'], 'group3' => ['user31', 'user32', 'user33'], 'group4' => ['user41', 'user42', 'user43'], 'group5' => ['user51', 'user52'], ]; ?>

Using two nested loops, output the elements of this array in the format group name - user name.

← →
↑
English▼
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline