Jump to content

Recommended Posts

You need {..} when embedding 2dim array references in quoted strings

 

<?php
$days=array();
$days = array(
    2=>array("hello","going") );

echo "{$days[2][1]}";	 // going
echo "{$days[2][0]}";	 // hello

?>

Arrays in PHP are 0-indexed, which means that in order to get the first value you have to reference index 0. Like this:

echo $days[2][0];

 

PS: Adding quotes around the variable like that is completely useless, as they should only be used if you're defining actual string content. The variable already contains strings, and even if it didn't PHP will automatically cast it to a string when using echo.

The first line is also not necessary, as you're overwriting it in the second. array () returns a new array object, so no matter what the variable contained earlier it'll be overwritten with the new array.

0-indexed means that 0 is the first number used in a count, while 1-indexed means that 1 is the first number used. Nothing (at least that I know) uses 2-indexed counting.

In normal life we're used to 1-based indexing, as we start to count everything at 1. PHP, however, starts to count at 0. So if you have 3 elements in an array, you'll find them at indices 0, 1, and 2.

 

A Google search should bring up lots more information on this subject, if you still have problems with understanding it.

check with you why need to put {} in front and at the end of echo array ?

 

Required for PHP to parse complex expressions within a string - see the section on "Complex Expressions" on this page of the manual

http://www.php.net/manual/en/language.types.string.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.