Problem with scope of arrays

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
denncarm
Forum Newbie
Posts: 1
Joined: Fri Jul 14, 2006 8:30 am

Problem with scope of arrays

Post 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]
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

array( 'Judge', 'Event', 'Level', 'Dance_num' ) makes $array[0] = 'Judge' ... Not $array['Judge']
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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 );
Post Reply