Members of an array, do show your self

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

User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Members of an array, do show your self

Post 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]. :lol:

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 :D

For all those brave and dedicated...

Thanks ahead! :lol:
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Try [php_man]implode[/php_man]() and see what it can do for you.

-- Scorphus
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Members of an array, do show your self

Post 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
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
(
    &#1111;0] =&gt; 1
    &#1111;1] =&gt; 2
    &#1111;2] =&gt; 3
    &#1111;3] =&gt; 4
    &#1111;4] =&gt; 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Re: Members of an array, do show your self

Post 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() example

Code: Select all

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>
Regards,
Scorphus.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Show me how you would get what I posted with implode then, I can't think of a way (mind block).
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post 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);

?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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"; 
} 
?>
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post 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;)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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

&#1111;~/tmp] on Tue Jun 29, 12:14:47 - 6
&#1111;scorphus]@&#1111;Kilauea] $ php -f tmp2.php
&lt;a href="page1.html"&gt;page1&lt;/a&gt;&lt;br /&gt;
&lt;a href="page2.html"&gt;page2&lt;/a&gt;&lt;br /&gt;
&lt;a href="page3.html"&gt;page3&lt;/a&gt;&lt;br /&gt;
&lt;a href="page4.html"&gt;page4&lt;/a&gt;&lt;br /&gt;
&lt;a href="page5.html"&gt;page5&lt;/a&gt;&lt;br /&gt;
&#1111;~/tmp] on Tue Jun 29, 12:14:48 - 6
&#1111;scorphus]@&#1111;Kilauea] $
-- Scorphus
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Seems a bit unesecary to add all that code for what a for loop and a string could do easily..
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

no extra pipe :roll:

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++;
} 
?>
Last edited by qads on Tue Jun 29, 2004 10:22 am, edited 1 time in total.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
Post Reply