Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2006, 05:22 AM
Registered User
 
Join Date: Dec 2006
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

 
Old December 29th, 2006, 06:25 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
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
 
Old December 29th, 2006, 06:29 AM
Authorized User
 
Join Date: Dec 2006
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

 
Old December 29th, 2006, 08:20 AM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, surendran and raaj.






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





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