PHPで指定した文字を持つデータで仕分けするためのコードを書いてみました。
名前とジャンルがあり、contentsに配列として入っています。
その中から、インスタンスのgenreにweb
を持つものだけに絞り込み$webs
に格納しようと試みたところ、配列の中に何も入らないことに気づきました。
どのような原因が考えられるでしょうか?
よろしくお願いいたします。
-blogs
--blog1.php
-program
--data.php
data.php ↓
php
1 2<?php 3class Hoge { 4 private $name; 5 private $genre; 6 7 public function __construct($name,$genre){ 8 $this->name = $name; 9 $this->genre = $genre; 10 } 11 12 public function getName(){ 13 return $this->name; 14 } 15 16 public function getGenre(){ 17 return $this->genre; 18 } 19 20 public static function sortGenre($contents,$what){ 21 $genres = []; 22 foreach ($contents as $cont){ 23 if ($cont->getGenre() == $what){ 24 $genres[] = $cont; 25 } 26 } 27 return $genres; 28 } 29} 30 31$hoge1 = new Hoge ("title1","web"); 32$hoge2 = new Hoge ("title2","web"); 33$hoge3 = new Hoge ("title3","web"); 34 35$contents = array($hoge1,$hoge2,$hoge3); 36 37$webs = Hoge::sortGenre($contents, "web"); 38
blog1.php↓
php
1 <?php require($_SERVER['DOCUMENT_ROOT'] ."/program/data.php") ?> 2 <?php foreach($webs as $web): ?> 3 <article> 4 <div> 5 <p><?php echo $web->getName() ?></p> 6 <p><?php echo $web->getGenre() ?></p> 7 </div> 8 </article> 9 <?php endforeach ?> 10 11
結果
php
1何も表示されない
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/03/13 17:06
2017/03/13 17:41
退会済みユーザー
2017/03/13 18:39