基于多个参数对对象数组进行排序

时间:2018-03-08 14:25:48

标签: php csv sorting object

我试图为一家活动公司制作一个小组成员的议程 - 他们的网站是用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;

    }

}

然后是一个空数组,我们将存储我们的Panelist对象     $ 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 ...
            }
        }
    }
}

?>

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

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

谢谢! : - )

1 个答案:

答案 0 :(得分:0)

你到了那里。

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

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>';
    }
}

输出:

纸币 约翰 奢摩他 简