そのため、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年があるかどうかを明確に述べ、それを設定してから、曲のタイトルの横に印刷すると思いました。
誰かが私を正しい方向に向けることができますか?