473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array value as array index

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
3 1305
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben Katz | last post by:
Is it possible to have a JavaScript object that works just like a standard Array, except that when the array is modified, a function gets called which can then do some processing on the array? ...
1
by: Ben Katz | last post by:
Is it possible to have an Array object (or an object derived from Array) that is 'aware' of other code modifying its contents? I'd like to have such an "onModify" function process an array...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
4
by: Arjen | last post by:
Hi, I need to add this inside an array: 1 3 2 4 3 2 4 5 5 1 I think of using this:
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.