【问题标题】:Sorting An Array of Objects Based on Multiple Parameters根据多个参数对对象数组进行排序
【发布时间】:2018-08-16 22:41:50
【问题描述】:

我正在尝试为一家活动公司制定小组成员的议程 - 他们的网站是用 PHP 制作的。他们已经有一个列出小组成员的 CSV 文件。我编写了一些代码,以便他们可以将 CSV 上传到他们的服务器并将其呈现为表格。

csv 或多或少是这样设置的:

Panel, Name, Last Name, Title, Company, Moderator
tuesday, John, Doe, Partner, Acme,1
tuesday, Jane, "O Reily", Partner, SkyNet,0
tuesday, Samatha, Klein, CEO, Sea World,0
tuesday, Bill, Clarke, Head of Marketing, TNT,0
wednesday, Mohammed, Algarisi, Managing Director, Cheesy Photos,1
wednesday, Tim, Draper, Founding and Managing Partner, Draper Associates,0

无论如何,他们希望小组成员按姓氏字母顺序排序,主持人首先显示。我在 PHP 中执行此操作时遇到问题。

我不太习惯 PHP 代码,所以我确定我一定缺少一些东西,我应该以不同的方式进行设置吗?最好的排序方法是什么?

这基本上就是我所做的-

首先我创建了一个小组成员课程:

class Panelist {

    function __construct($panel, $name, $lastname, $title, $company, $moderator){
        $this -> panel = $panel;
        $this -> name = $name;
        $this -> lastname = $lastname;
        $this -> title = $title;
        $this -> company = $company;
        $this -> moderator = $moderator;

    }

}

然后是一个空数组,我们将在其中存储 Panellist 对象 $panelists =array();

$row = 1;
//accesses our csv file from which we will get the data for the objects
if(($handle = fopen("agenda.csv", "r")) !== FALSE) {
    //loops through the csv file by row
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        //skips the header (first) row
        if($row == 1) {$row++; continue;}
        //instanciates a Panelist object for every row in the csv file
        $name = new Panelist($data[0], $data[1], $data[2], $data[3], $data[4], data[5] );
        //adds object to our $panelist array
        array_push($panelists, $name);

}

然后我有一个接收两个参数的输出函数:

1. $arr - the array where the objects are stored
2. $panelName - the name of the panel to output 


    function outputSpeakers($arr, $panelName){
 // loops through objects in $arr
    foreach($arr as $obj){
     //only outputs objects with a panel value matching $panelName:
        if($obj->panel == $panelName){
            $name = $obj->name;
            $lastname = $obj->lastname;
            $title = $obj->title;
            $company= $obj->company;
            //lots of condition formatting stuff here that's not important such as...
            if($obj->moderator == '1'){
               //if the moderator is "TBA" - don't output title or company:
                if($name == ' TBA'){
                    //format this way
                } //else ...
            }
        }
    }
}

?>

然后,在我的议程.php 文件中,我包含上面的类文件并执行:

<div class="panel-list">
    <? outputSpeakers($panelist, "tuesday"); ?>
</div>

谢谢! :-)

【问题讨论】:

    标签: php csv sorting object


    【解决方案1】:

    你快到了。

    简而言之,你可以试试这个:

    if ($obj->panel == $panelName) {
        $result[$obj->{'last name'}] = $obj;
    }
    
    ksort($result);
    

    但我会使用不同的方式来创建议程对象。

    <?php
    class Panelist {
        private $_agenda = null;
    
        public function put($csv) {
    
            if (!file_exists($csv)) {
                return false;
            }
    
            if (($handle = fopen($csv, "r")) !== FALSE) {
                $row = 1;
                $count = 0;
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if ($row == 1) {
                        $label = $data;
                        $count = count($data);
                    } else {
                        if (count($data) == $count) {
                            for($i = 0; $i < $count; $i++) {
                                $agenda[$row][trim(strtolower($label[$i]))] = trim(strtolower($data[$i]));
                            }                       
                        }
                    }
                    $row++;
                }
                fclose($handle);
                $this->_agenda = json_decode(json_encode($agenda));
                return $this;
            }
        }
    
        public function get($label, $value) {
            $result = [];
            if (!property_exists($this, '_agenda')) {
                return false;
            }
            foreach ($this->_agenda as $item) {
                // loop through agenda for match values by label
                if (!empty($item->{$label}) && !empty($item->{'last name'}) && $item->{$label} == $value) {
                    // arrange $result key with last name;
                    $result[$item->{'last name'}] = $item;
                }
            }
            // sort $result by key with ascending order
            ksort($result);
            return $result;
        }
    }
    
    $panelist = new Panelist;
    
    if ($agenda = $panelist->put("agenda.csv")->get('panel', 'tuesday')) {
        foreach ($agenda as $item) {
            echo $item->name . '<br>';
        }
    }
    

    输出:

    账单 约翰 定 简

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多