2014-10-17 95 views
-1

我正在查找与事件相对应的日期,并希望返回事件的名称。 这是一个使用生日的简单例子。PHP在多维二维数组中查找值

function find_event($month,$day){ 

    $event = array(
     ["Sam","1","1"], 
     ["Jim","2","3"], 
     ["Sally","3","11"], 
     ["Buck","4","4"], 
     ["Ted","12","22"] 
    ); // event name, month, day 

    /* what code goes here? */ 

    return "Happy Birthday, ".$event[$key][0]."!"; 
} 

find_event(12,22); 

回答

1

你可以试试这个:

<?php 
function find_event($month,$day){ 

    $event = array(
     ["Sam","1","1"], 
     ["Jim","2","3"], 
     ["Sally","3","11"], 
     ["Buck","4","4"], 
     ["Ted","12","22"] 
    ); // event name, month, day 

    foreach ($event as $e) { 
     if($e[1] == $month && $e[2] == $day) { 
      return "Happy Birthday, ". $e[0]."!"; 
     } 
    } 
    return null; 
} 

echo find_event(12,22); 
+0

我真的很喜欢这个。我没有测试过它,但它对我来说看起来很合适! – 2014-10-17 22:35:35

1

没有测试,但它必须是工作:

foreach ($event as $key => $e) { 
    if ($e[1] == $month && $e[2] == $day) { 
     $match = $key; 
     break; 
    } 
} 

if (!empty($match)) { 
    return $event[$match][0]; 
} 
+0

油滑,看起来像你和阿尔班在同一条轨道上! – 2014-10-17 22:36:13

+0

我没有看到它已经回答,但是,这是解决方案,但我现在编辑它,因为它不够合适。 :) – 2014-10-17 22:47:47

0

(跳到下跳过的解释,并与获得注释的代码“最好”的长期解决方案)

免责声明:这个东西不是一个小项目所必需的......但作为一个学习g练习我建议这样做。它从长远来看创造了更稳定,更灵活,更易读的代码。更不用说如果你以程序员的身份找到工作,你需要知道如何做下面的工作。

为此,您应该考虑使用类而不是数字索引的数组。另一种选择是使用关联数组。

使用关联数组:

function print_event($month, $day) 
{ 
    $events = [ 
     [ 
      "name" => "Sam", 
      "month" => "1", 
      "day" => "1" 
     ], 
     [ 
      "name" => "Jim", 
      "month" => "2", 
      "day" => "3" 
     ], 
     [ 
      "name" => "Sally", 
      "month" => "3", 
      "day" => "11" 
     ], 
     [ 
      "name" => "Buck", 
      "month" => "4", 
      "day" => "4" 
     ], 
     [ 
      "name" => "Ted", 
      "month" => "12", 
      "day" => "22" 
     ] 
    ]; // It is now clear exactly what is going on here. No need to tell me with a comment. 

    foreach ($events as $event) { 
     if ($month === $event["month"] && $day === $event["day"]) 
      return "Happy Birthday, ".$event["name"]."!"; 
    } 
    return "No birthdays today"; 
} 

上述方法效果很好,但它是一个快速ñ肮脏的解决方案。你应该使用对象:

function print_event($month, $day) { 
    $events = [ 
     new BirthdayEvent("Sam", "1", "1"), 
     new BirthdayEvent("Jim", "2", "3"), 
     new BirthdayEvent("Sally", "3", "11"), 
     new BirthdayEvent("Buck", "4", "4"), 
     new BirthdayEvent("Ted", "12", "22") 
    ]; //It is now clear exactly what is going on here. No need to tell me with a comment. 

    foreach ($events as $event) { 
     if ($event->isToday($month, $day)) { 
      return "Happy Birthday, " . $event->name . "!"; 
     } 
    } 
    return "No birthdays today"; 
} 

public class BirthdayEvent { 
    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    public function isToday($month, $day){ 
     return $this->month === $month && $this->day === $day; 
    } 
} 

这是好多了,但事情仍然可以改善!现在,打印事件只适用于你硬编码的一些事件,并且这些事件在内存中被重新实例化每次这个函数被调用!这悲剧!这可以快速地通过定义这些事件的代码外,然后将它们作为一个参数,像这样简单地加以解决:

$events = [ 
    new BirthdayEvent("Sam", "1", "1"), 
    new BirthdayEvent("Jim", "2", "3"), 
    new BirthdayEvent("Sally", "3", "11"), 
    new BirthdayEvent("Buck", "4", "4"), 
    new BirthdayEvent("Ted", "12", "22") 
]; //It is now clear exactly what is going on here. No need to tell me with a comment. 

function print_event($events, $month, $day) {... rest of code ...} 

现在这个越来越不错...但它可以更好的进行! print_event实际上是在做两件事......它既是打印事件又是事件的寻找。我们应该让查找事件成为它自己的功能。

现在我们的代码块看起来是这样的:

function print_event($events, $month, $day) { 
    $event = find_event($events, $month, $day); 
    if($event === null) 
     return "No birthdays today"; //No event was found. No birthday today 
    else 
     return "Happy birthday, ".$event->name."!"; //An event was found! Happy birthday! 
} 

function find_event($events, $month, $day) { 
    foreach ($events as $event) { 
     if ($event->isToday($month, $day)) { 
      return $event; //Yay we found an event! return it! 
     } 
    } 
    return null; // We didn't find anything. return null 
} 

这又是再好不过了!如果你把它分解,代码开始像英文一样阅读!我们已经走得很远,但这仍然相对不灵活。让我们修复...

// create our events 
$events = [ 
    new BirthdayEvent("Sam", "1", "1"), 
    new BirthdayEvent("Jim", "2", "3"), 
    new BirthdayEvent("Sally", "3", "11"), 
    new BirthdayEvent("Buck", "4", "4"), 
    new BirthdayEvent("Ted", "12", "22") 
]; //It is now clear exactly what is going on here. No need to tell me with a comment. 

//This function will print the greeting from the event 
function print_event(array $events, $month, $day) { 
    $event = find_event($events, $month, $day); 
    if($event === null) 
     return "No events today"; //No event was found. 
    else 
     return $event->getGreeting(); //An event was found! Happy birthday! 
} 

//This function will find an event with a given month/day from an array of events 
function find_event(array $events, $month, $day) { 
    foreach ($events as $event) { 
     if ($event->isToday($month, $day)) { 
      return $event; //Yay we found an event! return it! 
     } 
    } 
    return null; // We didn't find anything. return null 
} 

// This interface says, "Anything that implements me MUST HAVE THESE METHODS!" 
interface EventInterface { 
    public function isToday($month, $day); 
    public function getGreeting(); 
} 

// This class implements event by defining the methods in the Event interface 
public class BirthdayEvent implements EventInterface{ 

    protected $name; 
    protected $month; 
    protected $day; 

    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    // return true if the event is today 
    public function isToday($month, $day){ 
     return $this->month === $month && $this->day === $day; 
    } 

    public function getGreeting(){ 
     return "Happy birthday, ".$this->name."!"; 
    } 
} 

//Finally, echo the output 
echo print_event($events, "3", "11"); 

我们已经添加了一个接口。这个接口允许我们创建多种类型的事件,这些事件都与print_event和find_event函数保持兼容!看:

// This interface says, "Anything that implements me MUST HAVE THESE METHODS!" 
interface EventInterface { 
    public function isToday($month, $day); 
    public function getGreeting(); 
} 

// This class implements event by defining the methods in the Event interface 
public class BirthdayEvent implements EventInterface{ 

    protected $name; 
    protected $month; 
    protected $day; 

    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    // return true if the event is today 
    public function isToday($month, $day){ 
     return $this->month == $month && $this->day == $day; 
    } 

    public function getGreeting(){ 
     return "Happy birthday, ".$this->name."!"; 
    } 
} 

// This class implements event by defining the methods in the Event interface 
public class HolidayEvent implements EventInterface{ 
    protected $name; 
    protected $month; 
    protected $day; 

    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    // return true if the event is today 
    public function isToday($month, $day){ 
     return $this->month === $month && $this->day === $day; 
    } 

    public function getGreeting(){ 
     return "Happy ".$this->name."!"; //For example, it would print "Happy Columbus Day!" if it were columbus day 
    } 
} 

,现在我们的活动阵列可以是这样的:

// create our events 
$events = [ 
    new BirthdayEvent("Sam", "1", "1"), 
    new BirthdayEvent("Jim", "2", "3"), 
    new BirthdayEvent("Sally", "3", "11"), 
    new BirthdayEvent("Buck", "4", "4"), 
    new BirthdayEvent("Ted", "12", "22"), 
    new HolidayEvent("Columbus Day", "10", "13"), 
    new HolidayEvent("Labor Day", "9", "1") 
]; //It is now clear exactly what is going on here. No need to tell me with a comment. 

现在,这是伟大的,所有的,但有做的最后一件事...有相当多的重复代码这两个事件类之间!下面,我们介绍一个抽象类来忘记两次编写代码!

//This is an abstract class. On it's own, it serves no purpose. But it can be extended to reduce duplicate code! 
abstract class BaseEvent implements EventInterface { 
    protected $name; 
    protected $month; 
    protected $day; 

    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    // return true if the event is today 
    public function isToday($month, $day){ 
     return $this->month == $month && $this->day == $day; 
    } 
} 

// This class implements event by defining the methods in the Event interface 
public class BirthdayEvent extends BaseEvent { 
    public function getGreeting(){ 
     return "Happy birthday, ".$this->name."!"; 
    } 
} 

// This class implements event by defining the methods in the Event interface 
public class HolidayEvent extends BaseEvent{ 
    public function getGreeting() { 
     return "Happy ".$this->name."!"; //For example, it would print "Happy Columbus Day!" if it were columbus day 
    } 
} 

这可能是你想要做什么矫枉过正,但是这是去上有很多复杂的大型项目,这样的问题的最佳途径。利用这一点,你现在可以创建新的类型的事件,只是这样的:

public class SomeAwesomeNewTypeOfEvent extends BaseEvent { 
    public function getGreeting() { 
     return "Some awesome greeting goes here"; 
    } 
} 

这里的一切都将是什么样子时,它的说和做:

// create our events 
$events = [ 
    new BirthdayEvent("Sam", "1", "1"), 
    new BirthdayEvent("Jim", "2", "3"), 
    new BirthdayEvent("Sally", "3", "11"), 
    new BirthdayEvent("Buck", "4", "4"), 
    new BirthdayEvent("Ted", "12", "22"), 
    new HolidayEvent("Columbus Day", "10", "13"), 
    new HolidayEvent("Labor Day", "9", "1") 
]; //It is now clear exactly what is going on here. No need to tell me with a comment. 

//This function will print the greeting from the event 
function print_event(array $events, $month, $day) { 
    $event = find_event($events, $month, $day); 
    if($event === null) 
     return "No events today"; //No event was found. 
    else 
     return $event->getGreeting(); //An event was found! Happy birthday! 
} 

//This function will find an event with a given month/day from an array of events 
function find_event(array $events, $month, $day) { 
    foreach ($events as $event) { 
     if ($event->isToday($month, $day)) { 
      return $event; //Yay we found an event! return it! 
     } 
    } 
    return null; // We didn't find anything. return null 
} 

// This interface says, "Anything that implements me MUST HAVE THESE METHODS!" 
interface EventInterface { 
    public function isToday($month, $day); 
    public function getGreeting(); 
} 

//This is an abstract class. On it's own, it serves no purpose. But it can be extended to reduce duplicate code! 
abstract class BaseEvent implements EventInterface { 
    protected $name; 
    protected $month; 
    protected $day; 

    public function __construct($name, $month, $day){ 
     $this->name = $name; 
     $this->month = $month; 
     $this->day = $day; 
    } 

    // return true if the event is today 
    public function isToday($month, $day){ 
     return $this->month == $month && $this->day == $day; 
    } 
} 

// This class implements event by defining the methods in the Event interface 
public class BirthdayEvent extends BaseEvent { 
    public function getGreeting(){ 
     return "Happy birthday, ".$this->name."!"; 
    } 
} 

// This class implements event by defining the methods in the Event interface 
public class HolidayEvent extends BaseEvent{ 
    public function getGreeting(){ 
     return "Happy ".$this->name."!"; //For example, it would print "Happy Columbus Day!" if it were columbus day 
    } 
} 

//Finally, echo the output 
echo print_event($events, "3", "11"); 

让我知道如果你要我解释任何东西。