PDA

View Full Version : Accessing an array entry's item



ruffy
Oct 29th, 2009, 09:57 PM
I created an array, whose entries looks like this:

[41, "The bird flew into it's cage"]
[33, "He drew fire from Joe"]
[33, "Roger asked her her name"]
[2, "I am awfully happy"].

I want to pull the sentence item
out of some array entries.
E.g., in the second entry,
namely, arrayName[1],
what I thought was the
second item (the sentence),
I could manage by invoking
arrayName[1][1].

But to my dismay,
this doesn't do the trick.
arrayName[1][1] actually delivers up
the second CHARACTER of the
entry (the number "3").

I thought the comma between
the number and the sentence would
separate the items, although I
"pushed" each entry into the array
as one single entry.

How do I "grab" the whole sentence?
How do I push both items separately
to achieve "one entry"?

Old Pedant
Oct 29th, 2009, 10:38 PM
Show your real code.

Because this worked exactly as you wanted, for me:


<script>
var foo = [
[41, "The bird flew into it's cage"],
[33, "He drew fire from Joe"],
[33, "Roger asked her her name"],
[2, "I am awfully happy"]
];

alert( foo[1][1] );
</script>

Kor
Oct 29th, 2009, 10:40 PM
You should have had created the array as an array of arrays, which, probably, you have not.


<script type="text/javascript">
var array=[
[41, "The bird flew into it's cage"],
[33, "He drew fire from Joe"],
[33, "Roger asked her her name"],
[2, "I am awfully happy"]
]
alert(array[1][1]);
</script>

ruffy
Oct 30th, 2009, 04:53 PM
I should have clarified, I want to
select a few words out of the sentence.
Which means the sentence item of the entry
is itself an array, as Kor pointed out.
I thought, wrongly, that simply
putting square brackets around the sentence
would render the item as an array.

Here's my code Old Pedant, with some calculations
replaced by comments to eliminate overload.
<script language="javascript">
jsArray = new Array();
<?php
$file = fopen("./phpArray.txt","r");
$item1 = "";
$item2 = "";
$itemEntry = "";
while(! feof($file))
{
$row = fgets($file);
// Calculate $digits and $length
$item1 = substr($row, $digits, $length);
// Calculate $startat and $len
$item2 = "'".substr($row, $startat, $len)."'";
$itemEntry = "[".$item1.",'".$item2."']";
print "jsArray.push(\"$itemEntry\" );";
}
fclose($file);
?>
</script>I suppose item2 needs to be an array
on its own. How do I change these 3 lines:
$item2 = "";

$itemEntry = "[".$item1.",'".$item2."']";
print "jsArray.push(\"$itemEntry\" );";

Kor
Oct 30th, 2009, 05:24 PM
Even we might know php, wouldn't be much simpler to post the HTML generated code, not the php one? After all, no one, except you, might know what the php variables look like. For javascript the final written thing matters.

ruffy
Oct 30th, 2009, 06:11 PM
Kor, the HTML is irrelevant.
It contains nothing but body, head, title and html tags.
The javascript is entirely there for you to see.
The structure of the array I gave the first time.
If you have trouble with the PHP, perhaps
concentrate on the (only) 3 javascript lines in the
code.

Kor
Oct 30th, 2009, 06:22 PM
Kor, the HTML is irrelevant.
No, Sir. You misunderstood me. Show us how your javascript code looks like as when you open the View Source. We need to see what php physically writes on the page, not the php code. Was I clear now?

Whenever you want to use php (or any other server-side language) to write a javascript part of the code, you must think upside-down. First write, clear, the javascript code, test if it works, than later create the php code which will write the javascript code.

Now I guess we need to see what php writes down there.

You know, a lot of errors occur, usually, with the maneuver of the quotes, for instance...

By the way, as a marginal note. language is deprecated. Use type instead:


<script type="text/javascript">

Old Pedant
Oct 30th, 2009, 09:30 PM
> I want to select a few words out of the sentence.
Fine, JavaScript could easily do that, by breaking up the sentence on word boundaries.

> Which means the sentence item of the entry is itself an array

Huh?? No, it's not. Certainly not as you coded it.

> I thought, wrongly, that simply putting square brackets around the sentence
would render the item as an array

Even if that were true, you did *NOT* put square brackets around the *sentence*.
You put them around the combination of that number *and* the sentence:

[41, "The bird flew into it's cage"]

Not sure what the 41 is for. It's clearly not the length of the string. But I assume you have some use for it.

It would help a lot if you would tell us what you want to *do*--what you want the results to be. Along with showing us the HTML/JavaScript you are generating. (Incidentally, to back up Kor, JavaScript code embedded in an HTML page *IS* part of the HTML. Just as much as is, say, the title that you see between <title>...</title> tags. HTML doesn't care what you put between <script> and </script>, but it does preserve it for you.)

Old Pedant
Oct 30th, 2009, 09:32 PM
Oh, and by the way, your grammar is wrong.

It should be "the bird flew into its cage". No apostrophe.

<grin>Had to justify my moniker.</grin>