好的,所以我有一个类,我希望能够在另一个php脚本中初始化一个对象后得到任何trackName(二维数组)。这是相关课程的一部分:
class fetchData{
public $artistName;
public $trackName;
public function getArtistTracks(){
$i = 0;
if( $tracks = getTracks() ){
foreach ( $tracks['results'] as $track ){
// $artistName is an array of strings (defined as public)
$name[$this->artistName][$i] = $track['name'];
$i++;
}
}
return $name;
}
// later in the class one other function there is this
function initialize(){
...
...
$this->trackName = $this->getArtistTracks();
}
}
以下是我认为我可以从另一个脚本调用artistName和trackName的方法:
$temp = new fetchData();
echo $temp->artistName[3]; // this works (returns 'some_artist_name' for example)
echo $temp->trackName[$temp->artistName[3]][1]; // this doesn't work
echo $temp->trackName['some_artist_name'][1]; // this also doesn't work
我尝试了其他一些方法。例如,而不是:
$name[$this->artistName][$i] = $track['name'];
我会说:
$this->name[$this->artistName][$i] = $track['name'];
没有在getArtistTracks()中返回,并且没有在initialize()中的行,但这也不起作用。如何从另一个脚本中获取这些trackNames?
谢谢=)
答案 0 :(得分:0)
要关闭这个,函数应该看起来像这样($ i在错误的循环中):
public function getArtistTracks(){
if( $tracks = getTracks() ){
$i = 0;
foreach ( $tracks['results'] as $track ){
// $artistName is an array of strings (defined as public)
$name[$this->artistName][$i] = $track['name'];
$i++;
}
}
return $name;
}
答案 1 :(得分:0)
$ name [$ this-> artistName ---> [$ i]< ---] = $ track ['name'];
当然,只有$ temp-> artistName;是数组。