Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access to 2nd array's array - how?

Status
Not open for further replies.

tektippee

IS-IT--Management
Jun 15, 2008
6
0
0
US
I created an array ("jsArray"), 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 some words from the
sentence item in some entries.
E.g., in the second entry,
namely, jsArray[1],
do I relate to that sentence
by invoking jsArray[1][1]?

How do I "grab" the whole sentence?
Or some words in the sentence?
How do I make the sentence item
of the entry an array on its own?

Do I push both items separately
to achieve "one array entry"?

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>
Three javascript lines
constitute the entire javascript code.

How do I change these 3 lines:
$item2 = "";

$itemEntry = "[".$item1.",'[".$item2."']]";
print "jsArray.push(\"$itemEntry\" );";
 
Here are some examples that might help you. They are all client-side javascript, and attempt to show one way you might approach this problem. Note, I chose not to use push.

JavaScript:
var arr = [];
arr[arr.length] = [45, "The cat sat on the mat"];
arr[arr.length] = [22, "The dog broke the toy"];
arr[arr.length] = [67, "The employee was unable"];

var the_first_entry = arr[0]; // -> [45, "The cat sat on the mat"]
var the_number = the_first_entry[0]; // -> 45
var whole_sentence = the_first_entry[1]; // -> "The cat sat on the mat"

// return a word from a sentence
// pos is the position of the word in the sentence and is zero based
// if pos is too big, return the last word in the sentence
function getWordByPosition(str, pos) {
	var tmp = str.split(' ');
	return pos < tmp.length ? tmp[pos] : tmp[tmp.length - 1];
}

var first_word_in_first_entry = getWordByPosition(whole_sentence, 0); // -> "The"
var third_word_in_first_entry = getWordByPosition(whole_sentence, 2); // -> "sat"
var last_word_in_second_entry = getWordByPosition(arr[1][1], 9999); // -> "toy"

Let us know if you have any questions.

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top