⊗ppPmSFAArEI 273 of 447 menu

Inserting Associative Array Elements in PHP

However, inserting elements of associative arrays will not work like this:

<?php $arr = ['a'=>1, 'b'=>2, 'c'=>3]; echo "xxx $arr['a'] yyy"; // does not work ?>

To insert such elements, they must be wrapped in curly braces:

<?php $arr = ['a'=>1, 'b'=>2, 'c'=>3]; echo "xxx {$arr['a']} yyy"; ?>

Alternatively, you can remove the single quotes from the key during insertion:

<?php $arr = ['a'=>1, 'b'=>2, 'c'=>3]; echo "xxx $arr[a] yyy"; ?>

Sometimes it makes sense to simply write an array element into a variable in order to then easily insert the variable into the string:

<?php $arr = ['a', 'b', 'c']; $elem = $arr['a']; echo "xxx $elem yyy"; ?>

Simplify the following code:

<?php $arr = ['a' => 1, 'b' => 2, 'c' => 3]; echo 'text ' . $arr['a'] . ' text ' . $arr['b'] . ' text'; ?>
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