Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 29th, 2006, 05:22 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Index Numbers in Arrays

Hi,

I have just started learning PHP and I think that this may be a very stupid question (especially since I can't find anything on it here or on Google), but I'm having some problems with arrays.

I'm in Chapter Two of the Wrox Beginning PHP5 book (2004), and I am trying to write a function that calls values from the array. When I use, eg., $my_named_array["dog"], it works fine, but when I use exactly the same code but $my_named_array[0], no result is returned.

Below is the code for the working function:

Code:
<?php

$my_named_array = array("pig" => "ralph", "cat" => "scuzz", "bird" => "ickerbod");

$my_pig = $my_named_array["pig"];
$my_cat = $my_named_array["cat"];
$my_bird = $my_named_array["bird"];

echo "My pig is named $my_pig, my cat is named $my_cat and my bird is named $my_bird.";

?>
And for the non-working function:

Code:
<?php

$my_named_array = array("pig" => "ralph", "cat" => "scuzz", "bird" => "ickerbod");

$my_pig = $my_named_array[0];
$my_cat = $my_named_array[1];
$my_bird = $my_named_array[2];

echo "My pig is named $my_pig, my cat is named $my_cat and my bird is named $my_bird.";

?>
Oh, and on a tangential note, can I use capitals in arrays? And if not, is there a way to force capitals when echoing?

Thanks in advance,
JaneDean

Reply With Quote
  #2 (permalink)  
Old December 29th, 2006, 06:25 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: colombo, WP, Sri Lanka.
Posts: 481
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hai,
visit this link
http://www.w3schools.com/php/func_array.asp

http://www.google.lk/search?hl=en&q=php+array&meta=

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
Reply With Quote
  #3 (permalink)  
Old December 29th, 2006, 06:29 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

associated array(string array) cannot be used as numeric array

try this:
$my_named_array[0]="ralph";
$my_named_array[1]="scuzz";
$my_named_array[2]="ickerbod";

$my_pig = $my_named_array[0];
$my_cat = $my_named_array[1];
$my_bird = $my_named_array[2];

echo "My pig is named $my_pig, my cat is named $my_cat and my bird is named $my_bird.";

thanx
Raaj

Reply With Quote
  #4 (permalink)  
Old December 29th, 2006, 08:20 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, surendran and raaj.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays Moharo PHP How-To 2 April 7th, 2016 01:16 PM
arrays Abraham Java Basics 5 April 11th, 2009 11:10 PM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM



All times are GMT -4. The time now is 04:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.