xml - Relationship in php array -
i using gdata api youtube video , comments. reply in xml contains array inside it.
for video id , comments xml response different. example getting array id video id array , 1 id 1 or many comments in array.
array of both video id , comments follow:
foreach ($array $entry) { $videoid = basename($entry); $video[] = $videoid; $logger->info('response youtube:videoid=>' . $videoid); } $this->view->videoid = $video; $author = array(); $content = array(); $arraycnt = array(); foreach ($video $id) { $comment = "http://gdata.youtube.com/feeds/api/videos/".$id."/comments"; $sxml1 = simplexml_load_file($comment); $entries = $sxml1->entry; foreach ($entries $a) { $author[] = $a->author->name; $content[] = $a->content; } }
and particular view follow:
<table> <tr> <td> <?php for($i=0;$i<$length;$i++) { ?> <embed width="420" height="345" src="http://www.youtube.com/v/<?php echo $videoid[$i];?>" type="application/x-shockwave-flash"> </embed> <?php } ?> </td> <td> <?php foreach($content $cont) { ?> <p>comment:<?php echo $cont;?></p> <?php } ?> </td> <td> <?php foreach($author $auth) { ?> <p>commented by:<?php echo $auth;?></p> <?php } ?> </td> </tr> </table>
how can show video , comments in view like:
videoa1 commenta1 commenta2
videob1 commentb1 commentb2 commentb3
you can keep id of video in $author
, $content
arrays.
foreach ($video $id) { foreach ($entries $a) { $author[$id][] = $a->author->name; $content[$id][] = $a->content; } }
so can comment's authors specific video id:
foreach ($video $id) { echo $id; foreach($author[$id] $auth) { echo ' ', $auth; } }
the same goes comment's content.
of course can extend solution if want have 1 array comment's authors , content, logic stays same.
Comments
Post a Comment