Page 1 of 1

Problem with scope of arrays

Posted: Fri Jul 14, 2006 8:43 am
by denncarm
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] 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]


There is something about the scope of variables which I do not understand.

I am running PHP 5.1

I have a file which starts off with some html header information and the first php statements in the file are

Code: Select all

<?php

include 'lib.php';
include 'database.php';

$criteria = array( 'Judge', 'Event', 'Level', 'Dance_num' );
Followed by a combination of php and html code

Further down the file I have

Code: Select all

<?php
            $dance_num = $criteria['Dance_num'];        // error on this line
            echo "<INPUT TYPE=TEXT NAME=\"Dance_num\" value=$dance_num>";
         ?>
When I run it I get "Notice: Undefined index: Dance_num in ......"

What the heck am I missing I am certain that it is so simple that I am looking straight over
it and I really need to go to bed

Thanks in advance


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] 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]

Posted: Fri Jul 14, 2006 9:04 am
by jamiel
array( 'Judge', 'Event', 'Level', 'Dance_num' ) makes $array[0] = 'Judge' ... Not $array['Judge']

Posted: Fri Jul 14, 2006 9:08 am
by Bill H
As you have defined it, 'Dance_num' is not the index in the array, but rather is the value in the array at position 3 or, $criteria[3] = Dance_num.

To get 'Dance_num' as the index the array would need to be defined:

Code: Select all

$criteria = array( 'Judge'=>'Bill', 'Event'=>'Foxtrot', 'Level'=>4, 'Dance_num'=>4 );