#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    53
    Rep Power
    7

    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"?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,234
    Rep Power
    9400
    Sounds like you have an array of
    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]?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    53
    Rep Power
    7
    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\" );";

IMN logo majestic logo threadwatch logo seochat tools logo