Array Dump

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Array Dump

Post 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.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Array Dump

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

Code: Select all

<?php 
$features = explode(" - ","$notablefeatures"); 
foreach($features as $key => $value)
  print $key.'='.$value.'<br />';
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could also just do:

Code: Select all

<?php
foreach(explode('-', $notablefeatures) as $feature){
  echo "<ol>\n";
  echo   '<li>'.$feature."</li>\n";
}
?>
V
Forum Newbie
Posts: 3
Joined: Tue Mar 16, 2004 1:15 pm
Location: Keller, Tx.
Contact:

Post 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) &#123;
  echo "$feature\n";
&#125;
// will print out 
// this
// that
// sompn
// etc...

foreach ($features as $key => $feature) &#123;
  echo "$features&#1111;$key] is the same as $feature\n";
&#125;

for ($i=0; $i<count($features); $i++) &#123;
  echo "$features&#1111;$i]\n";
&#125;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ohh Okay I understand it now. Thanks alot Guys.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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>';
?>
Post Reply