Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 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 October 3rd, 2003, 09:12 AM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jams30
Default Returning index values from an associated array

Hi

Can anyone help with this question - on page 172 (chapter 5), there is a section that explains that a variable can be assigned to an index in an associated array and by doing this, the value of that index for its associated element can be returned, I know that this is is not part of the Try it outs, but would like to know how this would work when needed in the future.

Many thanks


Jamal

 
Old October 3rd, 2003, 04:02 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well the variable may be used in the indice.

Consider the following...
Dynamic creation of form text fields.

$i = 0;

$index[$i] = $i;
$text[$i] = "First Name: ";
$name[$i] = "first_name";
$value[$i] = $_POST["first_name"];
$type[$i] = "text";

$i++;

$index[$i] = $i;
$text[$i] = "Last Name: ";
$name[$i] = "last_name";
$value[$i] = $_POST["last_name"];
$type[$i] = "text";

for ($n = 0; $n <= $i; $n++)
{
    echo "{$text[$i]}<input type='{$type[$i]}' name='{$name[$i]}' value='{$value[$i]}' /><br />\n";
}

The variable $i creates numeric indices in the arrays, and the for loop accesses the array using the last value of variable $i.


$foo = "Hello, World!";

$this_array["{$foo}"];

is the same as

$this_array["Hello, World!"];

No real limitations on this. For more information have a look at the PHP manual page on array syntax.

http://www.php.net/manual/en/language.types.array.php

Your question was kind of vague. I hope this gives you a little more insight into what is possible!

: )
Rich




:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 3rd, 2003, 05:24 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Jams,
I just noticed a typo in my code

The $n variable is incremented and stopped when it is equal to the value of $i.

for ($n = 0; $n <= $i; $n++)
{
    echo "{$text[$n]}<input type='{$type[$n]}' name='{$name[$n]}' value='{$value[$n]}' /><br />\n";
}

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 7th, 2003, 09:23 AM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jams30
Default

Thanks Rich

I know I was a little vague, in my question - in fact I found what I was looking for further in the book, where it mentions using the Key/Current and List/Each functions to return the value assigned to the index of an associative variable. But thanks for your help and advice on this!!

Alas, I carry on....

Regards

Jamal

 
Old October 7th, 2003, 11:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are other ways to get the value of an array index. If you're looping through the entire array, I prefer to use the foreach construct instead of using while(list() = each()). It's much more readable, and probably is a little more efficient, too.

foreach($array_var as $key => $val)
{
    echo "The array index is $key and the value is $val.\n";
}

also, look at array_key(), array_keys(), and array_key_exists()
  http://www.php.net/array_key
  http://www.php.net/array_keys
  http://www.php.net/array_key_exists


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Function returning an array rfstuck Pro VB 6 5 March 12th, 2008 07:35 AM
Returning the Index number of an array prusila Java Basics 1 February 13th, 2007 05:54 AM
Why is DataRow returning 1 more index rsearing ASP.NET 2.0 Basics 1 January 14th, 2007 02:18 AM
All Array is not returning [Coding Error] vinod_yadav1919 Javascript How-To 0 January 18th, 2005 10:10 AM
Array index rajanikrishna Beginning PHP 1 December 3rd, 2003 02:00 PM





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