简体   繁体   English

从PHP中的关联数组获取匹配记录

[英]Get matching records from associative array in PHP

I query a database in PHP which will get results like the following table. 我用PHP查询数据库,其结果如下表所示。 These are stored in an associative array. 这些存储在关联数组中。

查询返回的数据

What I now want to be able to do is select only the records by a certain work_type and return the same type of associative array . 我现在想做的是仅选择具有特定work_type的记录,并返回相同类型的associative array For example "Part Time" . 例如"Part Time" Then I want to be able to sort these records according to whatever column I decide. 然后,我希望能够根据我决定的任何列对这些记录进行排序。 How would I do this? 我该怎么做?

This is as far as I got... 就我所知...

$data = $_SESSION['results']; //the database query results

foreach($data as $key => $row)
{
    if(in_array($row['work_type'], array('Part Time')))
    {
        $type[$key] = $row['work_type'];
        $date_posted[$key] = $row['created_at'];
    }
}
array_multisort($type, SORT_ASC, $date_posted, SORT_ASC, $data);

This doesn't work because the $data array which I send to multi-sort is a now different size to the ones I created. 这不起作用,因为我发送给multi-sort的$ data数组的大小现在与我创建的大小不同。

Ok I resolved this by just getting the data again from the database. 好的,我只是通过再次从数据库中获取数据来解决了这一问题。 Apparently this is more efficient. 显然,这更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM