Symfony2在树枝中显示关联数组

时间:2015-03-06 14:10:21

标签: twig associative-array

我是Symfony2框架的新手,我正在尝试从lastfm API解析一些XML并向用户显示信息。这将是每个项目的专辑标题,playcount和专辑图像的格式。

到目前为止,我可以向用户显示所有这些信息,但这并不是很有用,因为我打算在页面中添加CSS样式。任何建议将不胜感激。

这是我的控制器

     /**
 * @Route("/lastfm/albums", name="albums")
 * @Template()
 */
public function albumsAction()
{

$albumsclass = new Album();

   // pull in artist albums
  $albums = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?            method=artist.gettopalbums&artist=imagine+dragons&api_key=370f98844440c2ecc8e5f7        c6cea8a7a4');
$rank = $albums->xpath('/lfm/topalbums/album/@rank');

$album_name_array=array();
$album_playcount_array=array();
$album_url_array=array();
$album_image_array=array();

foreach ($rank as $ranks){

    foreach ($ranks as $rank_id) {

        $album_name = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/name');
        $album_playcount = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/playcount');
        $album_url = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/url');
        $album_image = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/image[4]');



    }
    $album_name = implode($album_name);  
    array_push($album_name_array,$album_name);

    $album_playcount = implode($album_playcount);
    array_push($album_playcount_array,$album_playcount);

    $album_url = implode($album_url);
    array_push($album_url_array,$album_url);

    $album_image = implode($album_image);
    array_push($album_image_array,$album_image);


}







$container=array();
for($i=0; $i<sizeof($album_name_array); $i++) {
                 array_push($container,$album_name_array[$i],$album_playcount_array[$i],$album_ur     l_array[$i],$album_image_array[$i]);
 }


//$hello = array('album_name'=>$album_name_array,
//           'album_playcount'=>$album_playcount_array,
//            'album_url'=>$album_url_array,
//            'album_image'=>$album_image_array,);
//array_push($album_name_array,$album_playcount_array);


return $this->render('AcmelastfmBundle:Default:albums.html.twig', array(
       // 'pageData' => array(
       //  'artistxml' => $artistxml,

        'rank' => $rank,
        'ranks' => $ranks,
        //'rank_id' => $rank_id,
        // 'ranks' => $ranks,
         'album_name' => $album_name_array,
         //'album_playcount' => $album_playcount_array[$i],
         'album_url' => $album_url_array,
         'album_image' => $album_image_array,
         'container' =>$container,
         'data' => var_export($container, true),
        //  
       //  'hello' => $hello,
        // 'james' => array('album_name' => $albumsclass->getAlbumName()),
     //    ),
        ));



}

这是我的观点

 {% extends '::lastfmbase.html.twig' %}
 {% block title %}Albums{% endblock %}

 {% block body %}


{% for key in container %}
{{key}} <br>
{% endfor %}<br>
{% endblock %}

我基本上试图将PHP中的代码转换为symfony2。但是,当我得到一个数组到字符串转换错误时,我找不到将关联数组值传递给twig的方法

<?php
// pull in artist albums
$albums = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?     method=artist.gettopalbums&artist=imagine+dragons&api_key=370f98844440c2ecc8e5f7 c6cea8a7a4');
$rank = $albums->xpath('/lfm/topalbums/album/@rank');

foreach ($rank as $ranks){

    foreach ($ranks as $rank_id) {
        $album_name = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/name');
        $album_playcount = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/playcount');
        $album_url = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/url');
        $album_image = $albums->xpath('/lfm/topalbums/album[@rank="'.$rank_id.'"]/image[4]');
    }



    $album_name = implode($album_name);
    $album_playcount = implode($album_playcount);
    $album_url = implode($album_url);
    $album_image = implode($album_image);

    print_r($rank_id);

    ?>
    <article class="album">
    <?php
    echo "<a href=\"".$album_url."\" target=\"_blank\">".$album_name."</a><br>";
    echo $album_playcount." listeners<br>";
    echo "<div><a href=\"".$album_url."\" target=\"_blank\"><img src=\"".$album_image."\" title=\"$album_name\" /></a></div><br>";
    ?>
    </article>
    <?php

}

1 个答案:

答案 0 :(得分:1)

我不确定你在问什么。 你是说这个吗?

{% for key, value in container %}
   {{ key }}: {{ value }}
{% endfor %}