dtbrd80422 2018-04-04 11:00
浏览 54

PHP按过滤查询的参数组数组

I got three classes :

    class libro {

       var $id; 
       var $name;
       var $editorial;
       var $materia = array();

       [... getters and setters ]

    class editorial {

       var $name; 
       var $id;
       [...]

    class materia {

       var $name; 
       var $id;
       [...]

This is working on a website catalogue, where I got an amount of books I get from an xml file. That works well, I checked it; i receive xml values good.

On my website's catalogue, I got a aside bar. By default, are shown all editorials and materias (categories), as well as all books.

So, when I click on a checkbox of one editorial/materia, page reloads with a new query from xml, where one of them are filtered. Books in catalogue are shown, but on the aside, I need to show only the element I checked in, and the other are grouped researching in the new xml query data.

For example, If I select editorial Great Books, page will reload showing only that one editorial on the checkbox, and on aside's section about Materias I will see all that are contained on the array(materia) inside every book, grouped by to not show repeated categories.

The other way of the example :

If I search by a Materia first, editorials on new query will be grouped by too. If I got Editorial selected and I click a category, only category and editorial selected will be shown, and upside down If I selected first the category.

I'm working with $_GET['editorial'] and $_GET['materia'] to work on new query data.

This is my code about grouping...

$bookList = array();

// Here are methods where I set xml data to classes, and I get an array (link below)

$groupedBookList = array();

foreach ($bookList as $book){

    // ERROR IN LINE BELOW (Undefined offset: 0 in ...)
    $groupBL_Element = &$groupedBookList[$book->get_editorial()->get_id() . "_" .$libro->get_materia(0)->get_id() ]; 
    $groupBL_Element['editorial'] = $book->get_editorial()->get_id();
    $groupBL_Element['materia'] = $book->get_materia(0)->get_id();

}

return array_values($groupedBookList);

goo.gl/ 397Wuz

Someone knows some way to group this? Thanks

The error I get is :

Undefined offset: 0 in (line selected by mine up)

  • 写回答

1条回答 默认 最新

  • du1913 2018-04-04 11:05
    关注

    I'm not sure if this is which you are looking for, but if you add to your foreach:

    foreach ($bookList as $book){
    
    $groupBL_Element = &$groupedBookList[$book->get_editorial()->get_id() . "_" .$libro->get_materia(0)->get_id() ];
    $groupBL_Element['editorial'] = $book->get_editorial()->get_id();
    $groupBL_Element['materia'] = $book->get_materia(0)->get_id();
    
    $groupedBookList[] = $groupBL_Element;
    

    }

    Then you can return $groupedBookList with our your elements grouped.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入