sungpeng Posted November 17, 2012 Share Posted November 17, 2012 $days=array(); $days = array( 2=>array("hello","going") ); echo "$days[2][1]"; I can't get the value "hello" out. Please help.. Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/ Share on other sites More sharing options...
Barand Posted November 17, 2012 Share Posted November 17, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393248 Share on other sites More sharing options...
sungpeng Posted November 17, 2012 Author Share Posted November 17, 2012 check with you why need to put {} in front and at the end of echo array ? Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393250 Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393253 Share on other sites More sharing options...
sungpeng Posted November 17, 2012 Author Share Posted November 17, 2012 Could you explain further on 0-indexed ? Is 1-indexed come with " " ? How about 2-indexed ? Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393258 Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393262 Share on other sites More sharing options...
Barand Posted November 17, 2012 Share Posted November 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393263 Share on other sites More sharing options...
sungpeng Posted November 17, 2012 Author Share Posted November 17, 2012 thank you both.. Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393266 Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/270835-echo-multi-dimensional-array-out/#findComment-1393279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.