0

そのため、PHPのデータ構造の主なソースを理解しようと努めている間、私は多くの配列ベースの質問をしてきました。

私は現在、アーティストとその曲のリストを吐き出すクラスの構築に取り組んでいます。3曲ごとに日付が横にあります。この配列に見られるように:

$music = array(
    'Creed' => array(
        'Human Clay' => array(
            array(
                'title' => 'Are You Ready'
            ),
            array(
                'title' => 'What If'
            ),
            array(
                'title' => 'Beautiful',
                'date' => '2012'
            ),
            array(
                'title' => 'Say I'
            ),
            array(
                'title' => 'Wrong Way'
            ),
            array(
                'title' => 'Faceless Man',
                'date' => '2013'

            ),
            array(
                'title' => 'Never Die'
            ),
            array(
                'title' => 'With Arms Wide pen'
            ),
            array(
                'title' => 'Higher',
                'date' => '1988'
            ),
            array(
                'title' => 'Was Away Those Years'
            ),
            array(
                'title' => 'Inside Us All'
            ),
            array(
                'title' => 'Track 12',
                'date' => '1965'
            ),
        ),
    ),
);

私が書いたのは次のとおりです。

class Music{

    protected $_music = array();

    protected $_html = '';

    public function __construct(array $music){
        $this->_music = $music;
    }

    public function get_music(){
        $year = '';
        $this->_html .= '<d1>';

        foreach($this->_music as $artist=>$album){
            $this->_html .= '<dt>' . $artist . '</dt>';
            foreach($album as $track=>$song){
                foreach($song as $songTitle){
                    if(isset($songTitle['date']) && !empty($songTitle['date'])){
                        $year = '['.$songTitle['date'].']';
                    }
                    $this->_html .= '<dd>' . $songTitle['title'] . $year. '</dd>';

                }
            }
        }
        $this->_html .= '</d1>';

    }

    public function __toString(){
        return $this->_html;
    }

}

$object = new Music($music);
$object->get_music();

echo $object;

私の質問は、次のようなものが戻ってくるということです。

Creed
    Are You Ready
    What If
    Beautiful[2012]
    Say I[2012]
    Wrong Way[2012]
    Faceless Man[2013]
    Never Die[2013]
    With Arms Wide pen[2013]
    Higher[1988]
    Was Away Those Years[1988]
    Inside Us All[1988]
    Track 12[1965]

ご覧のとおり、配列内のほとんどすべての曲の横に日付がありますが、そうではありません。私の質問は何の取引ですか?私は自分のループの中で、この曲に1年があるかどうかを明確に述べ、それを設定してから、曲のタイトルの横に印刷すると思いました。

誰かが私を正しい方向に向けることができますか?

4

3 に答える 3

0

設定後にリセットすることはない$yearため、配列で最初の年の値に遭遇すると、新しい値が来るまでその同じ年を常に使用します。

                if(isset($songTitle['date']) && !empty($songTitle['date'])){
                    $year = '['.$songTitle['date'].']';
                } else {
                    $year = ''; /// reset year to blank
                }

そして、やや無関係なものでは、これはおそらくタイプミスです:

    $this->_html .= '<d1>';
                       ^--- number one? not letter L for a `<dl>` tag?
于 2013-01-03T16:46:08.420 に答える
0

foreachループで年をリセットしていないため、値が再割り当てされるまで、最後に設定された年が使用されます。これが修正された音楽クラスです。

class Music{

protected $_music = array();

protected $_html = '';

public function __construct(array $music){
    $this->_music = $music;
}

public function get_music(){
    $year = '';
    $this->_html .= '<d1>';

    foreach($this->_music as $artist=>$album){
        $this->_html .= '<dt>' . $artist . '</dt>';
        foreach($album as $track=>$song){
            foreach($song as $songTitle){
                if(isset($songTitle['date']) && !empty($songTitle['date'])){
                    $year = '['.$songTitle['date'].']';
                }
                $this->_html .= '<dd>' . $songTitle['title'] . $year. '</dd>';
                $year = "";
            }
        }
    }
    $this->_html .= '</d1>';

}

public function __toString(){
    return $this->_html;
}

}

于 2013-01-03T16:46:17.677 に答える
0

曲の日付が設定されていない場合、year変数は前の曲(設定された曲)の値を保持しています。year日付が設定されているかどうかに関係なく、実際にはタイトルの横に印刷するように指示しています。

:にelse句が必要ですif

if(isset($songTitle['date']) && !empty($songTitle['date'])){
    $year = '['.$songTitle['date'].']';
} else {
    $year = '';
}

yearこれは、日付が設定されていない場合にクリアされます。

于 2013-01-03T16:49:31.943 に答える