开发者

PHP class public parameter as two dimensional array, how?

Ok, so I have a class and I would like to be able to get any trackName (two dimensional array) after initializing an object in another php script. This is a part of the class that is relevant:

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();
}
}

Here is how I thought I could call artistName and trackName from another script:

$temp = ne开发者_JS百科w 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

I tried a few other ways. For example instead of this:

$name[$this->artistName][$i] = $track['name'];

I would put this:

$this->name[$this->artistName][$i] = $track['name'];

without the return in getArtistTracks() and without the line in initialize() but that didn't work also. how can I get these trackNames from within another script?

thank you =)


to close this one up, the function should look like this ($i was in the wrong loop):

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;
}


$name[$this->artistName ---> [$i] <--- ] = $track['name'];

of course, only if $temp->artistName; is array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜