0

我正在尝试使用 php implode 将我的 php 数组转换为 html。

这是我的代码:

$myarray = array(); 
while ($row = mysql_fetch_assoc($result)) { 
$myarray[] = array("title"=>$row['title'], 
       "name"=>$row['title'], 
       "content"=>$row['content'],
       "image" => 
           array(
             "cls"=>"slide-image",
             "_src"=>$row['src'],
             "source"=>$row['source']                
             )   
       );
}

$rows = array_fill( 0, count( $myarray[0] ), "" );
$keys = array_keys( $myarray[0] );
foreach( $myarray as $k => $record )
    for( $i=0, $max=count( $rows ); $i < $max; $i++ )
        $rows[ $i ] .= $record[ $keys[ $i ] ];
print implode( "", $rows );

输出是

title 1, title-2, content for title 1, content for title 2ArrayArray

我想作为

title 1, content for title 1, title 2, content for title 2

而且我不知道为什么阵列会来。请问有什么帮助吗?

4

4 回答 4

1

给定数据库中的以下示例数组:

$data = array(
    array(
        'title' => 'Title 1', 
        'name' => 'Title 1', 
        'content' => 'Content 1', 
        'image' => array(
            'src' => 'a', 
            'title' => 'b', 
            'alt' => 'c'
        )
    ), 
    array(
        'title' => 'Title 2', 
        'name' => 'Title 2', 
        'content' => 'Content 2', 
        'image' => array(
            'src' => 'a', 
        )
    )
);

以下代码将遍历它:

$rows = array();
foreach($data as $row) {
    $img = '<img ';
    foreach($row['image'] as $attr => $value) {
        $img .= $attr . '="' . $value . '" ';
    }
    $img .= '/>'; //Close $img
    $rows[] = $row['title'] . ', ' . $row['content'] . ', ' . $img;
}
print implode(', ', $rows);

生产:

Title 1, Content 1, <img src="a" title="b" alt="c" />, Title 2, Content 2, <img src="a" />

更新

您可以在从数据库中提取数据时执行此操作:

$rows = array();
while($row = mysql_fetch_assoc($result)) {
    $img = '<img class="' . $row['cls'] . '" src="' . $row['src'] . '" title="' . $row['source'] . '" />';
    $rows[] = $row['title'] . ', ' . $row['content'] . ', ' . $img;
}
print implode(', ', $rows);
于 2012-12-04T18:05:57.557 回答
0

尝试将数组表示为字符串将产生字符串Array。在您的情况下,它是两个 $record['image'] 字段。尝试添加这个

$rows = array_fill( 0, count( $myarray[0] ), "" );
$keys = array_keys( $myarray[0] );
foreach( $myarray as $k => $record )
    for( $i=0, $max=count( $rows ); $i < $max; $i++ ){
        if (is_array($record[$keys[$i]])){
            $rows[$i].=implode("",$record[$keys[$i]]);
        }
        else $rows[ $i ] .= $record[ $keys[ $i ] ];
    }
print implode( "", $rows );

您可以使子数组的特定处理更加复杂,但这只是一个示例。

于 2012-12-04T17:36:30.973 回答
0

假设您从数据库中获取这些结果,我提供以下解决方案:

$myData = array();

while ($row = mysql_fetch_array($result)) {
    $myData[]['title']  = "<h2>".$row['title']."</h2>";
    $myData[]['content']    = "<p>".$row['content']."</p>";
    $myData[]['imageData']  = array("<img src='".$row['src']."' class='slide-image' />",$row['source']);
}

从这里,您可以使用 foreach 循环遍历 $myData。

于 2012-12-04T17:37:54.953 回答
0

由于 OP 的评论他们也想要图像而更新。

你也可以稍微调整一下我已经给你的例子来获取图像。$myarray只需在填充数据库中的值后立即放置这段代码。我还添加了 HTML 标记,以使我的示例对您来说更直接。

$html = '';
foreach($myarray as $record){
    if($html)
        $html .= '<hr/>';
    $html .= '<h1>' . $record['title'] . '</h1>';
    $html .= '<p>' . $record['content'] . '</p>';

    $image = $record['image'];
    $html .= '<img class="' . $image['cls'] . '" src="' . $image['_src'] . '" title="' . $image['source'] . '"/>';
}
于 2012-12-04T17:37:58.223 回答