开发者

How to make an array linkable that's using implode?

My array displays properly but I would like for the link inside to display the current value of the array.

Here is my code:

foreach( $persons as $pid => $p)
{
    echo '<a href="?tag=">' . im开发者_Python百科plode( '</a>, <a href="?tag=">', $tags[ $p['id'] ])  . '</a>';
    echo '<br /><br />';
}

This is what I want to display:

<a href="?tag=tag1">tag1</a>, <a href="?tag=tag2">tag2</a>

UPDATE I got the answer elsewhere. Turns out it was pretty simple. Going to accept answer that's helped me improve on my code.

$tags_arr = $tags[$p['id']];    
    foreach($tags_arr as $v){
  $out[] = "<a href='?tag=$v'>$v</a>";
}


I'm assuming $tags is an array itself, and you are attempting to write out each tag for each $p['id']. If I have it correct, don't use implode() for this. Instead use two foreach loops.

foreach ($persons as $pid => $p) {
  foreach ($tags as $t) {
    echo "<a href='?tag={$t[$p['id']]}'>{$t[$p['id']]}</a>\n";
  }
}

UPDATE

I see some problems here:

    $persons[$row['id']]['title'] = $row['title'];
    $persons[$row['id']]['height'] = $row['height'];
    $persons[ $row['id'] ] = array( 'id' => $row['id'], 'tag' => $row['tag']);

Above, you set the title and height array keys to $persons[$row['id']]. Following that though, you overwrite the whole $persons[$row['id']] with a new array(). Instead since you're keeping the same array keys you can simply use:

$persons[$row['id']] = $row;

Now where I believe the most serious problem is:

$tags[ $row['id'] ][] = $row['tag'];

By using the [] notation, you are appending $row['tag'] onto the $tags[ $row['id'] as an array element rather than setting its value to the tag. That's the reason you're getting Array(1) in place of the tag value. Instead use:

$tags[$row['id']] = $row['tag'];


use http_build_query — Generate URL-encoded query string

SYNTAX : string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

Returns a URL-encoded string.

<?php
    $data = array('foo'=>'bar',
                  'baz'=>'boom',
                  'cow'=>'milk',
                  'php'=>'hypertext processor');

    echo http_build_query($data) . "\n";
    echo http_build_query($data, '', '&amp;');

    ?>

The above example will output:

foo=bar&baz=boom&cow=milk&php=hypertext+processor

foo=bar&baz=boom&cow=milk&php=hypertext+processor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜