Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2008, 10:45 PM   PM User | #1
bryceowen
New to the CF scene

 
Join Date: Aug 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bryceowen is an unknown quantity at this point
'echo'ing a value from a 2D array?

Is there any way to echo a value from a 2D array? When I try:
PHP Code:
echo "$array_2d[0][0]"
I get:

Array[0]

But if I pull it to a variable first then echo the variable, it displays correctly. Is there any way to directly echo the array value or does it have to be this way? It isn't a problem, but I'd like to eliminate any extra, unnecessary variables from my code.
bryceowen is offline   Reply With Quote
Old 09-11-2008, 11:10 PM   PM User | #2
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
The quotes are unneeded and increases the load time of your script:

PHP Code:
echo $array_2d[0][0]; 
Mwnciau is offline   Reply With Quote
Old 09-11-2008, 11:12 PM   PM User | #3
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
if it's 2d you don't need to define the vertical dimension you can just do

echo $array_2d[0];
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 09-12-2008, 12:12 AM   PM User | #4
bryceowen
New to the CF scene

 
Join Date: Aug 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bryceowen is an unknown quantity at this point
Mwnciau: I tried your suggestion and nothing displays.

hinch: I tried your suggestion and it just returns "Array".
bryceowen is offline   Reply With Quote
Old 09-12-2008, 06:16 PM   PM User | #5
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
<?php
$darr = array('one','two','three','four');
echo $darr[0];
?>

works just fine take the number out of your variable atm you have array_2d change it to $arraytwod and it should work fine

appears it doesn't like numbers in array var names don't ask me why i dont know
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 09-12-2008, 09:09 PM   PM User | #6
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
Quote:
Originally Posted by bryceowen View Post
Mwnciau: I tried your suggestion and nothing displays.

hinch: I tried your suggestion and it just returns "Array".
Hinch, you are talking about a 1d array not 2d.

Bryce, are you sure there is anything in 0, 0? This:

PHP Code:
<?php

$array_2d 
= array ( array ( '0, 0''0, 1''0, 2' ), '1', array ( '2, 0''2, 1' ) );

echo 
$array_2d[0][0] . '<br />';
echo 
$array_2d[2][1];
outputs this:

Code:
0, 0
2, 1
Mwnciau is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:42 PM.


Advertisement
Log in to turn off these ads.