Page 1 of 2
Members of an array, do show your self
Posted: Sun Jun 27, 2004 10:07 am
by Calimero
INTRO:
I use this piece of the code:
Code: Select all
<?php
$var1 = explode(" ", $var);
foreach ($var1 as $var)
{
//do something
}
$print_var = $var1[0].$var1[1].$var1[2];
print "$print_var";
?>
The code is joining members of an array.
THE PROBLEM:
But this code only joins first 3 of an who knows how long array.
I need the code that will join all members of the array (but not in a way that i need to write/code $var1[150].
So is there any way this can be done, and plus with the members of the array I need to assign-join a hyperlink
Code: Select all
<?php
EX. <a href=''>$var[1]</a>
?>
If any points I missed, do shoot - My firewall will (hopefully) do the job
For all those brave and dedicated...
Thanks ahead!

Posted: Sun Jun 27, 2004 10:18 am
by scorphus
Try [php_man]implode[/php_man]() and see what it can do for you.
-- Scorphus
Re: Members of an array, do show your self
Posted: Mon Jun 28, 2004 9:43 am
by pickle
Code: Select all
<?php
$var1 = explode(" ", $var);
$print_var; // <- Add this
foreach ($var1 as $var)
{
//do something
$print_var .= $var; // <- And this
}
print "$print_var"; // <- and change this accordingly
?>
Posted: Tue Jun 29, 2004 7:45 am
by scorphus
Hi pickle, did you read my post and follow that link? [php_man]implode[/php_man]() does exactly what you did in that foreach loop, look:
Code: Select all
<?php
$array = range(1, 5); // [php_man]range[/php_man]()
$string = implode('', $array); // [php_man]implode[/php_man]()
print_r($array); // [php_man]print_r[/php_man]()
echo $string . "\n";
?>
what outputs:
Code: Select all
Array
(
ї0] => 1
ї1] => 2
ї2] => 3
ї3] => 4
ї4] => 5
)
12345
Of course, your example is correct, and I understand that it is a good example to show an apprentice or beginner how things work and how he can develop his own techniques.
But, most of the times, this doesn't play a role. Commonly we are in a production scenery. Imagine that we have to do this several times in multimple parts of our application. We better place this peace of code inside a function and call it whenever we need it.
In this case, [php_man]implode[/php_man]() does the un-exploding job in an easier (that part would be played by our function), clearer (just a function with couple args), shorter (one line) and faster (the code is already compiled and linked inside php lib) manner. Not to mention that the code would be more compatible and already documented.
PHP has many functions that perform tasks like this, hackneyed, common tasks of ours day by day.
Regards,
Scorphus.
Posted: Tue Jun 29, 2004 8:46 am
by d3ad1ysp0rk
and if you want it to look like:
<a href="$var[0].html">$var[0]</a><br />
<a href="$var[1].html">$var[1]</a><br />
?
Then implode won't work.
Re: Members of an array, do show your self
Posted: Tue Jun 29, 2004 9:05 am
by scorphus
Calimero wrote:(...) The code is joining members of an array. (...)
That's what it is all about.
implode() function reference wrote:implode
(PHP 3, PHP 4 , PHP 5)
implode --
Join array elements with a string
Description
string implode ( string glue, array pieces)
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
Example 1. implode() exampleCode: Select all
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?>
Regards,
Scorphus.
Posted: Tue Jun 29, 2004 9:15 am
by d3ad1ysp0rk
Show me how you would get what I posted with implode then, I can't think of a way (mind block).
Posted: Tue Jun 29, 2004 9:43 am
by qads
you cant, implode puts the whole array into single string, its usefull if you dont wanna loop to get the array values out, but thats about it.
Posted: Tue Jun 29, 2004 9:55 am
by leenoble_uk
cat amongst pigeons
Code: Select all
<?php
foreach($someArray as $key=>$value)
{
$someNewArray[] = "<a href='whatever.php?var=".$key."'>".$val."</a>\n";
}
$code = implode("",$someNewArray);
?>
Posted: Tue Jun 29, 2004 9:57 am
by qads
why not just do this
Code: Select all
<?php
foreach($someArray as $key=>$value)
{
$code .= "<a href='whatever.php?var=".$key."'>".$val."</a>\n";
}
?>
Posted: Tue Jun 29, 2004 10:09 am
by leenoble_uk
Ok in that scenario, I'll grant you there's no real reason but something I've used it for in the past is text navigation.
Say you want a nice even spacing of text navigation or you want a pipe character between options like:
Code: Select all
Option One | Option Two | Option three | Option Four
Using your method you'd end up with an extra pipe at the end of the navigation.
Code: Select all
<?php
foreach($navigationaArray as $key=>$val)
{
$textNavArray[] = "<a href='whatever.php?var=".$key."'>".$val."</a>\n";
}
$navigation = implode(" | ",$textNavArray);
?>
Without using implode you'd have to go back and lop off the extra pipe (and space if you were using nbsp;)
Posted: Tue Jun 29, 2004 10:15 am
by scorphus
I think this would be a good approach:
Code: Select all
<?php
function drawLink ($val) {
return("<a href="page${val}.html">page$val</a><br />");
}
$array = range(1, 5);
$string = implode("\n", array_map("drawLink", $array)); // [php_man]array_map[/php_man]()
echo $string . "\n";
?>
output:
Code: Select all
ї~/tmp] on Tue Jun 29, 12:14:47 - 6
їscorphus]@їKilauea] $ php -f tmp2.php
<a href="page1.html">page1</a><br />
<a href="page2.html">page2</a><br />
<a href="page3.html">page3</a><br />
<a href="page4.html">page4</a><br />
<a href="page5.html">page5</a><br />
ї~/tmp] on Tue Jun 29, 12:14:48 - 6
їscorphus]@їKilauea] $
-- Scorphus
Posted: Tue Jun 29, 2004 10:18 am
by d3ad1ysp0rk
Seems a bit unesecary to add all that code for what a for loop and a string could do easily..
Posted: Tue Jun 29, 2004 10:21 am
by qads
no extra pipe
Code: Select all
<?php
$size = sizeof($someArray) - 1;
foreach($someArray as $key => $value)
{
$code .= "<a href='whatever.php?var=".$key."'>".$value."</a>";
if($x < $size)
{
$code .= " | \n";
}
$x++;
}
?>
Posted: Tue Jun 29, 2004 10:21 am
by scorphus
Me agrees

, but you asked for it:
LiLpunkSkateR wrote:Show me how you would get what I posted with implode then, I can't think of a way (mind block).
-- Scorphus