[Resolved] help forming&selecting info from a multidim a

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elmcitizen
Forum Newbie
Posts: 3
Joined: Sun Apr 02, 2006 11:06 am

[Resolved] help forming&selecting info from a multidim a

Post by elmcitizen »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello all, i'm having some trouble selecting information from a multidimensional array.

I'm reading in the array information from a text file, the information is formatted as so:

Code: Select all

3 1
1 2
4 1
4 1
4 1
I build the array as so:

Code: Select all

//get Transition Table
for ($counter = 0; $counter < $rowsTrans; $counter++)
{
$tableTrans = fgets($fp);
$tableTrans = trim($tableTrans);
//split string into array
$TransTable = explode(" ", $tableTrans);
//array into multidimensional array
$transTableArray[$counter] = $TransTable;
print_r($TransTable);
echo "<br>";
}
print_r($transTableArray);
The $rowsTrans is selected from the text file earlier and is returning the correct result.

The output of print_r($transTable Array) is:

Code: Select all

Array ( [0] => Array ( [0] => 3 [1] => 1 ) [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [0] => 4 [1] => 1 ) [3] => Array ( [0] => 4 [1] => 1 ) [4] => Array ( [0] => 4 [1] => 1 ) )
Now to select the information from the array I'm doing this:

Code: Select all

//get value from Transitional Table
$column = current( $usrStringArray);
echo "Column: $column <br>";
$row = $currentState;
echo "Row: $row <br>";
$currentState = $transTableArray[$row][$column];
echo "Test: $transTableArray[$row][$column]<br>";
echo "Results from Trans Table Inquiry (last Char): $currentState <br>";
Its output is:

Code: Select all

Column: 0 
Row: 0 
Test: [0]
Why is it returning 0? I know I'm in $transTableArray[0][0] but its value should be 3...

If anyone could help me at all it would be appriciated, I'm on AIM as elmcitizen as well.
Thanks Again!


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by elmcitizen on Sun Apr 02, 2006 12:32 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[0] shows because php will only know to parse the first array indexing, not both. This is due to how string are parsed.

use the following instead

Code: Select all

echo "Test: {$transTableArray[$row][$column]}<br>";
// or
echo 'Test: ' . $transTableArray[$row][$column] . '<br>';
elmcitizen
Forum Newbie
Posts: 3
Joined: Sun Apr 02, 2006 11:06 am

Post by elmcitizen »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


First, Thank you for editing my original post to comply with the rules for posting.

Second, 
I edited the code, but now it is not returning any result..

Code: Select all

//get value from Transitional Table
		$column = current( $usrStringArray);
		echo "Column: $column <br>";
		$row = $currentState;
		echo "Row: $row <br>";
		$currentState = $transTableArray[$row][$column];
		echo "Test1: {$transTableArray[$row][$column]}<br>"; 
		// or 
		echo 'Test2: ' . $transTableArray[$row][$column] . '<br>';
		echo "Results from Trans Table Inquiry (last Char): $currentState <br>";
with the out of:

Code: Select all

Column: 0 
Row: 0 
Test1: 
Test2:
All I'm really looking to do is assign the value of the particular array cell to a variable called $current state.
is the code:

Code: Select all

$currentState = $transTableArray[$row][$column];
Am I doing this properly? is there a better way to search the array? Unfortunately my textbook doesn't cover this...


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it would appear $transTableArray doesn't exist at that point.

try adding

Code: Select all

error_reporting(E_ALL);
to the top of your code.
elmcitizen
Forum Newbie
Posts: 3
Joined: Sun Apr 02, 2006 11:06 am

Post by elmcitizen »

I figured it out finally, i forgot to declare a variable as an int!!!!
DOH!
Your original method now works properly, Thanks Again!
Post Reply