Page 1 of 1
Array Dump
Posted: Mon Mar 15, 2004 1:09 pm
by John Cartwright
What is the best way to get all the variables in an array??
ie
Code: Select all
<?php
$features = explode(" - ","$notablefeatures");
?>
I was to display these in a list.
Re: Array Dump
Posted: Mon Mar 15, 2004 1:12 pm
by TheBentinel.com
Phenom wrote:What is the best way to get all the variables in an array??
I think you're looking for "foreach"
http://us2.php.net/foreach
Lets you go through all the elements of an array one at a time.
Posted: Mon Mar 15, 2004 1:21 pm
by John Cartwright
Looking at the manual it gives this
Code: Select all
<?php
<?php
$arr = array("one", "two", "three");
reset ($arr);
while (list(, $value) = each ($arr)) {
echo "Value: $value<br />\n";
}
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
?>
?>
Looking at that it shows taht you have to declare all the variables inside the array. But I do not know how many different variables "$features[]" there are going to be? Am I being stupid or what :S Help ty
Posted: Mon Mar 15, 2004 3:45 pm
by penguinboy
Code: Select all
<?php
$features = explode(" - ","$notablefeatures");
foreach($features as $key => $value)
print $key.'='.$value.'<br />';
?>
Posted: Mon Mar 15, 2004 5:49 pm
by pickle
You don't know how many are going in? If that's the case, you can always use $features[] when adding, and PHP will automatically add the value to the next index of $array. If you need to know how many elements are going to be in $features before you start reading, you can always use count($features).
Posted: Tue Mar 16, 2004 10:15 pm
by John Cartwright
This is what I've come up with, its pretty farfetched at the moment but here it is..
Code: Select all
<?php
$features = explode(" - ","$notablefeatures");
$numfeatures = count($features);
echo $numfeatures;
$numbfeaturesend = $numfeatures + 1;
$icount=0;
while ( $icount < $numbfeaturesend ) {
echo "<ol>\n";
echo "<li>".$features[$icount]."</li>\n";
$icount++;
}
?>
Using $numfeatures = count($features); I get 1 when displaying its value.
Also the explode isnt working.. it is supposed to seperate all the lines with a - but its not, when echo'ing $features it comes out like it is in the database.. ie.. hello 1 - hello 2 - hello 3
EDIT * ROFL i just had to change " - " to "-" in the explode function
Posted: Tue Mar 16, 2004 10:25 pm
by markl999
You could also just do:
Code: Select all
<?php
foreach(explode('-', $notablefeatures) as $feature){
echo "<ol>\n";
echo '<li>'.$feature."</li>\n";
}
?>
Posted: Tue Mar 16, 2004 10:34 pm
by V
Dont quote the array when you're passing it to explode.
Code: Select all
$notablefeatures = "this-that-sompn-else-other-than-woohoo";
// note there's no quotes around $noteablefeatures, below
$features = explode('-', $notablefeatures);
echo count($features);
// will print out "7" as there are 7 dash seperated values in the string above
foreach ($features as $feature) {
echo "$feature\n";
}
// will print out
// this
// that
// sompn
// etc...
foreach ($features as $key => $feature) {
echo "$featuresї$key] is the same as $feature\n";
}
for ($i=0; $i<count($features); $i++) {
echo "$featuresї$i]\n";
}
Posted: Tue Mar 16, 2004 10:40 pm
by John Cartwright
Ohh Okay I understand it now. Thanks alot Guys.
Posted: Tue Mar 16, 2004 11:01 pm
by m3mn0n
[php_man]foreach[/php_man]() is nice for handling the array data, but if you simply want to look at them (say for investigating GET vars, or whatever); [php_man]print_r[/php_man]() is great.
eg.
Code: Select all
<?php
echo '<pre>';
print_r ($_SERVER);
echo '</pre>';
?>