THE ANSWER THAT WORKED FOR ME BASED ON AN ANSWER GIVEN
while($post = mysql_fetch_array($tags)) {
$push = explode(',', $post['tags']);
$array = array_merge($array, $push);
}
So I’m trying to display tags from my data base and make links out of them like this:
<?
$tags = mysql_query( 'SELECT tags FROM `Table`');
$array = array();
while($post = mysql_fetch_array($tags)) {
$push = explode(',', $post['tags']);
array_push($array, $push);
}
foreach ($array as $value) {?>
<a href="url.php?tags=<? echo $value?>"><? echo $value?></a>
<? }
?>
However all I get back is
<a href="url.php?tags=Array">Array</a>
Where I should be having at least three lines as was previously produced by
<?
$tags = mysql_query( 'SELECT tags FROM `Table`');
while($post = mysql_fetch_array($tags)) {
$array = explode(',', $post['tags']);
foreach ($array as $value) {?>
<a href="url.php?tags=<? echo $value?>"><? echo $value?></a>
<? }
}
?>
The code being called looks like this:
tag1, tag2, tag3
Tried
while($post = mysql_fetch_array($tags)) {
$push = explode(',', $post['tags']);
array_merge($array, $push);
}
foreach ($array as $value) {?>
<a href="index.php?tags=<? echo $value?>"><? echo $value?></a>
now foreach doesn’t return a value
Use
array_merge(), becausearray_push()will push the output ofexplode(), which is an array, as a whole to the array in the first argument, creating a jagged array.As for your edit, this works:
Please note that
array_merge()(contrary toarray_push(), gotta love the consistency) does not alter the array passed as its first argument, so you’ll have to store the return value which I do on the first line ($array = ...).While outputting to HTML, you also might want to put a
$value = htmlentities(trim($value));in theforeachloop.