The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Accessing an array entry's item
Discuss Accessing an array entry's item in the JavaScript Development forum on Dev Shed. Accessing an array entry's item JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 29th, 2009, 02:50 PM
|
|
Contributing User
|
|
Join Date: Jul 2008
Posts: 53
Time spent in forums: 13 h 11 m 53 sec
Reputation Power: 5
|
|
|
Accessing an array entry's item
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"?
|

October 29th, 2009, 03:17 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Sounds like you have an array of
javascript Code:
Original
- javascript Code |
|
|
|
var arrayName = [ '[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"]' ];
What's arrayName[1][0]?
|

October 30th, 2009, 09:58 AM
|
|
Contributing User
|
|
Join Date: Jul 2008
Posts: 53
Time spent in forums: 13 h 11 m 53 sec
Reputation Power: 5
|
|
I should have clarified Requinix, I want to
select a few words out of the sentence.
Which means the sentence item of the entry
is itself an array.
I thought, wrongly, that simply
putting square brackets around the sentence
would render the item as an array.
Here's my code, with some calculations
replaced by comments to eliminate overload.
Code:
<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\" );";
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|