PHP is quickly becoming more complicated than most complex
typed variable languages for me. I need some more basic
education.
In almost every tutorial I have seen, examples of using variables
as array index references take the form $array[$index]. In point of fact
this never works. For example:
<?
$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
$name = 'Big';
$surname = 'Bopper';
echo 'Name = ' . $user[$name] . $user[$surname];
?>
Will only produce:
Name=
The Apache error_log only indicates 'undefined index:'
In another example, there seems to be a 'type' problem:
<?
$theday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_of_week = date("w");
echo "$day_of_week <br>";
echo $theday[$day_of_the_week];
?>
produces '5' (as day of the week) and the Apache error_log indicates:
undefined variable: day_of_the_week line 5
undefined index: line 5
At least in this second example, it occurred to me that maybe the
array index had to be an integer rather than a string. If I substitute
echo $theday[5]; for echo $theday[$day_of_the_week];
the script works correctly and produces "Friday" in the output.
I thought that PHP was somewhat like Python or Java and type
conversion was somewhat automatic or at least you didn't have to
worry as much about it. This apparently is not the case.
What do I have to do to make these references work correctly?
1. Can I convert the string to an integer with a built-in PHP function in
the second example? ( I have yet to find it in the manual)
2. Why won't the first example work since the index is already
a string?
TIA
Variables as array index references
Moderator: General Moderators
-
pootergeist
- Forum Contributor
- Posts: 273
- Joined: Thu Feb 27, 2003 7:22 am
- Location: UK
$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
$name = 'Big';
$surname = 'Bopper';
echo 'Name = ' . $user[$name] . $user[$surname];
?>
you're using the value rather than the key. $user['Bopper'] doesn't exist, whereas $user['surname'] does - so
$name = 'name';
$surname = 'surname';
would be the correct way to go.
and...
$day_of_week = (INT) date("w");
should do for the other.
$name = 'Big';
$surname = 'Bopper';
echo 'Name = ' . $user[$name] . $user[$surname];
?>
you're using the value rather than the key. $user['Bopper'] doesn't exist, whereas $user['surname'] does - so
$name = 'name';
$surname = 'surname';
would be the correct way to go.
and...
$day_of_week = (INT) date("w");
should do for the other.
the indices for this array are name and surname$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
Big and Bopper are the values for those keys
Code: Select all
<?php
$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
?>
the array looks like
<pre><?php print_r($user); ?></pre>
<hr />
accessing single elements:
<pre><?php echo $user['name'], "\n", $user['surname'], "\n"; ?></pre>
<hr />
accessing single elements with a variable index
<pre><?php
$key = 'name';
echo $user[$key], "\n";
$key = 'surname';
echo $user[$key], "\n";
?></pre>
<hr />
the keys/indices of the array are;
<pre><?php print_r(array_keys($user)); ?></pre>
the values of the array are:
<pre><?php print_r(array_values($user)); ?></pre>
<hr />
iterating the array:
<pre><?php
foreach($user as $key=>$value)
echo $key, ': ', $value, "\n";
?></pre>Err, excuse the idiocy.
I should probably practice what I preach and step away from the
keyboard when I have spent more than a hour trying to figure
out why something doesn't work.
Thanks
A final question:
Several examples use the form;
echo 'Name = ' . {$user[$name][$surname]} . '<br>';
using the curly braces to surround what is described as a complex
variable. This never works as expected. Is this legitimate code and
I am somehow just misusing it or is it improper?
This actually reminds me of accessing a two-tiered array rather than
the simple arrays found in PHP.
I should probably practice what I preach and step away from the
keyboard when I have spent more than a hour trying to figure
out why something doesn't work.
Thanks
A final question:
Several examples use the form;
echo 'Name = ' . {$user[$name][$surname]} . '<br>';
using the curly braces to surround what is described as a complex
variable. This never works as expected. Is this legitimate code and
I am somehow just misusing it or is it improper?
This actually reminds me of accessing a two-tiered array rather than
the simple arrays found in PHP.