【问题标题】:How to return multiple results in a php function如何在一个php函数中返回多个结果
【发布时间】:2015-08-19 15:19:09
【问题描述】:

我有以下函数,旨在使用来自 url ($id) 的 ID 和在网页上显示信息的 foreach 语句从艺术家那里获取特定歌曲的所有学分。目前它可以很好地显示艺术家姓名,但没有显示 ID。我将如何返回 ID 信息以使其也显示出来?

function getArtistsBySongId($id)
{
$query = "SELECT * FROM `Credit_To_Artist` AS c2a 
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
LEFT OUTER JOIN `Song` AS s ON s.song_id = c2a.song_id
LEFT OUTER JOIN `Project` AS p ON p.project_id = s.project_id
WHERE c2a.song_id = $id";

$res = mysql_query($query);

$artists = Array();
$artisttoid = Array();
$songtoid = Array();

while( $row = mysql_fetch_array($res) ) {
    $artist = $row[artist_name];
    $credit = $row[credit_name];
    $songcr = $row[song_id];

    if(!array_key_exists($artist, $artists) ) {
        $artists[$artist] = Array();
        $artisttoid[$artist] = $row[artist_id];
        $songtoid[$songcr] = $row[song_id];
    }

    $artists[$artist][] = $credit;

}

return $artists;
return $songtoid;
return $artisttoid;

}

我在代码中使用了 include,因为我对 PHP 还很陌生,并且发现它更容易理解。

<table border="0" cellspacing="5" cellpadding="5" class="cdinfo" width="100%;">
    <tr>
        <?php
        if (getArtistsBySongId($id) == NULL) {
            echo "<th style='font-size: 13px'>Credits:</th>";
            echo "<td style='font-size: 13px'>There are currently no artists linked to this song.</td>";
        } else {
            include 'songs/getsongcredits.php';
        }
        ?>
    </tr>
</table>

歌曲/getsongcredits.php

<?php foreach (getArtistsBySongId($id) as $artist => $creditarr) {
        $credits = implode( ", ", $creditarr );
        echo "<a href='star.php?id={$artisttoid[$artist]}'>{$artist}</a> ({$credits})<br />";
} ?>

【问题讨论】:

标签: php function foreach return


【解决方案1】:

我建议您返回一个对象,其中包含您想要的属性中的每个项目。 要返回一个对象,首先你需要一个类:

class MyClass {
    private $artists;
    private $songtoid;
    private $artisttoid;

    public function __construct($arg1, $arg2, $arg3){
        $this->artists = $arg1;
        $this->songtoid = $arg2;
        $this->artisttoid = $arg3;
    }

    public function getArtists(){return $this->artists;}
    public function getSongtoid(){return $this->songtoid;}
    public function getArtisttoid(){return $this->artisttoid;}
}

在你的函数中

function getArtistsBySongId(){
    ...
    return new MyClass($artists, $songtoid, $artisttoid);
}

你也可以像这样返回一个关联数组

return array(
    "artists"=>$artists,
    "songtoid"=>$songtoid,
    "artisttoid"=>$artisttoid
);

或者,如果您愿意,您可以返回一个数组(如 Machavity 所回答的那样)并使用 list() 读取结果

function getArtistsBySongId(){
    ...
    return array($artists, $songtoid, $artisttoid);
}

list($artists, $songtoid, $artisttoid) = getArtistsBySongId();

【讨论】:

  • 感谢您回复@Francisco Pacheco。但是,上面的数组返回示例在我的代码中似乎不起作用,您能举一个如何返回对象的示例吗?
  • 要返回一个对象,首先你需要一个类: class MyClass { public $artists;公共 $songtoid;公共 $artisttoid;公共函数 __construct($arg1, $arg2, $arg3){ $this->artists = $arg1; $this->songtoid = $arg2; $this->artisttoid = $arg3;在此之后,您可以返回对象: return new MyClass($artists, $songtoid, $artisttoid);
  • 我编辑了答案,现在它有返回对象的代码
【解决方案2】:

对象或数组是这样做的方法

return array('artists' => $artists, 'songtoid' => $songtoid, 'artisttoid' => $artisttoid);

【讨论】:

  • 感谢您的回复@machavity。我在我的代码中尝试了这个解决方案,但输出变得很奇怪。例如,艺术家 (artist_id 18) 的歌曲 (song_id 455) 返回结果: 0 (Array) 1 (455) 2 (18) 你知道出了什么问题吗?
  • 试试我上面的代码,对返回值做一个var_dump(),看看是否可行
  • 数组所做的只是保存您的数据以供返回。你仍然必须正确地接受它并输出它。就像我说的,var_dump()它然后输出它
  • 能否请教使用var_dump()的例子并输出?我查看了 PHP.net,但不确定将其放置在哪里。很抱歉成为一个痛苦的人。 ://
  • var_dump(getArtistsBySongId($id)); 它自己输出
猜你喜欢
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2021-05-27
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多