FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


An easy way to echo all parts of these variables.





Dougie1
Well the code I have has an unknown number of variables. OK. I have $count-1 number of variables which can all be accessed like this:

Code:
$variable[1] or $variable[2]


How would I echo all of the variables, 1 all the way up to $count-1 in the most efficient way possible with a comma separating each. (least code)
sonam
I am not sure, but maybe you are looking for something like that:

Code:
for($a=0; $a<$count; $a++) {
        $something = @$something . $variable[1] . ", ";
}

echo $something;


Sonam
MrBlueSky
Code:

  echo implode(", ", $variable);
kv
Code:
vardump($variable)
mariohs
You can use the print_r function:

From php.net site:

php.net wrote:
print_r

(PHP 4, PHP 5)

print_r Prints human-readable information about a variable

Description
bool print_r ( mixed $expression [, bool $return] )

Note: The return parameter was added in PHP 4.3.0

print_r() displays information about a variable in a way that's readable by humans. If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects. print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5.

Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.


Code:
print_r (your_array_variable);
SlowWalkere
Some other examples (like the implode) may be the shortest solution, code-wise. However, I think the most proper and useful solution (for future situations) is to use foreach().

Code:
foreach ($variable as $key => $value)
{
    echo $var;
    echo ", ";
}


Foreach() takes an array (like your $variable[]) of undetermined length and cycles through each piece of data. In the definition above, $value is the value of $variable[1] and $key would be 1.

This will prove very useful if you have a similar situation but want to do something more complicated than simply echo the data. For example, if you manipulate the data in some way (i.e. keep a running total), implode won't work and you'll need to actually iterate through your array in some kind of loop.

Good luck,
- Walkere
Related topics
File upload with PHP, Build an upload database for your site
Mambo CMS
PHP what is it actually?
Computers at school
A PHP Script Contest, winner get 100F$
PHP Problem
AJAX tutorial [2nd part now updated]
Why this code dont work ?
500 FRIH$: For a custom-made storefront. (PHP)
Firefox 2.0 Beta 2 Released!
Need Expert help in php-mysql
Help with php and java script
Helical Electron Path
Where does life come from? in Science matter
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.