By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,636 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,636 IT Pros & Developers. It's quick & easy.

array value as array index

P: n/a
This code
$cn = $ct[$k];
print "<a name='$ct[$k]'>$continentname[$cn]</a>\n";
works, and prints something like <a name='af'>Africa</a>

But if I try to simplify it, to
print "<a name='$ct[$k]'>$continentname[$ct[$k]]</a>\n";
I get a syntax error
unexpected '[', expecting ']'

So what is the correct way to write something like $a[$b[$c]] ? I have
tried adding extra {} and '' here and there, so far without success.

Nick
--
Nick Wedd ni**@maproom.co.uk
Jan 25 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
On Thu, 25 Jan 2007 14:13:50 +0100, Nick Wedd <ni**@maproom.co.ukwrote:
This code
$cn = $ct[$k];
print "<a name='$ct[$k]'>$continentname[$cn]</a>\n";
works, and prints something like <a name='af'>Africa</a>

But if I try to simplify it, to
print "<a name='$ct[$k]'>$continentname[$ct[$k]]</a>\n";
I get a syntax error
unexpected '[', expecting ']'

So what is the correct way to write something like $a[$b[$c]] ? I have
tried adding extra {} and '' here and there, so far without success.
Curly braces should work. Tried & tested:
<?php
$bar = 'bar';
$foo[$bar] = 'baz';
$foz['baz'] = 'success';
echo "{$foz[$foo[$bar]]}";
?>

So use print "<a name='$ct[$k]'>{$continentname[$ct[$k]]}</a>\n";
--
Rik Wasmus
* I'm testing several new newsreaders at the moment. Please excuse
possible errors and weird content. *
Jan 25 '07 #2

P: n/a
Message-ID: <op***************@misant.kabel.utwente.nlfrom Rik
contained the following:
>So what is the correct way to write something like $a[$b[$c]] ? I have
tried adding extra {} and '' here and there, so far without success.

Curly braces should work. Tried & tested:
<?php
$bar = 'bar';
$foo[$bar] = 'baz';
$foz['baz'] = 'success';
echo "{$foz[$foo[$bar]]}";
?>
Or concatenation
print "<a name='$ct[$k]'>".$continentname[$ct[$k]]."</a>\n";
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jan 25 '07 #3

P: n/a
In message <d7********************************@4ax.com>, Geoff Berrow
<bl******@ckdog.co.ukwrites
>Message-ID: <op***************@misant.kabel.utwente.nlfrom Rik
contained the following:
>>So what is the correct way to write something like $a[$b[$c]] ? I have
tried adding extra {} and '' here and there, so far without success.

Curly braces should work. Tried & tested:
<?php
$bar = 'bar';
$foo[$bar] = 'baz';
$foz['baz'] = 'success';
echo "{$foz[$foo[$bar]]}";
?>

Or concatenation
print "<a name='$ct[$k]'>".$continentname[$ct[$k]]."</a>\n";
Thanks to both of you.

I am impressed by how positive and helpful the responses in this
newsgroup are.

Nick
--
Nick Wedd ni**@maproom.co.uk
Jan 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.