简体   繁体   English

如何在symfony中对对象进行排序?

[英]How can I sort an object in symfony?

This is "fields" 这是“领域”

{#751 ▼
  +"id": array:9 [▼
    "fieldName" => "id"
    "type" => "integer"
    "scale" => 0
    "length" => null
    "unique" => true
    "nullable" => false
    "precision" => 0
    "id" => true
    "columnName" => "id"
  ]
  +"username": array:8 [▼
    "fieldName" => "username"
    "type" => "string"
    "scale" => 0
    "length" => 25
    "unique" => true
    "nullable" => false
    "precision" => 0
    "columnName" => "username"
  ]
  +"unique_id": array:8 [▼
    "fieldName" => "unique_id"
    "type" => "string"
    "scale" => 0
    "length" => 10
    "unique" => true
    "nullable" => false
    "precision" => 0
    "columnName" => "unique_id"
  ]
  +"password": array:8 [▼
    "fieldName" => "password"
    "type" => "string"
    "scale" => 0
    "length" => 64
    "unique" => false
    "nullable" => false
    "precision" => 0
    "columnName" => "password"
  ]
  +"email": array:8 [▼
    "fieldName" => "email"
    "type" => "string"
    "scale" => 0
    "length" => 191
    "unique" => true
    "nullable" => false
    "precision" => 0
    "columnName" => "email"
  ]
  +"isActive": array:8 [▼
    "fieldName" => "isActive"
    "type" => "boolean"
    "scale" => 0
    "length" => null
    "unique" => false
    "nullable" => false
    "precision" => 0
    "columnName" => "is_active"
  ]
}

I would like that username is always at the beginning and password is always at the end. 我希望用户名始终位于开头,密码始终位于结尾。

Here is my approach: 这是我的方法:

  usort($fields, function ($a, $b) {  if ($a['fieldName'] == 'username' || $b['fieldName'] == 'password') return -1;
        elseif ($a['fieldName'] == 'password' || $b['fieldName'] == 'username') return 1;
        else return 0;
      });

But I get an error message: 但是我收到一条错误消息:

Warning: usort() expects parameter 1 to be array, object given 警告:usort()期望参数1为数组,给定对象

The first parameter of usort should be an array and $fields is an object. usort的第一个参数应该是一个数组,而$fields是一个对象。 One option could be to cast it to an array like: 一种选择是将其强制转换为如下数组:

$fields = (array)$fields;

And then pass it to usort: 然后将其传递给usort:

usort($fields, function ($a, $b) {
    if ($a['fieldName'] == 'username' || $b['fieldName'] == 'password') return -1;
    elseif ($a['fieldName'] == 'password' || $b['fieldName'] == 'username') return 1;
    else return 0;
});

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

相关问题 如何使用Symfony Finder组件按DESC顺序对文件进行排序? - How can I sort files by DESC order with Symfony Finder Component? 如何在laravel上对物体进行排序? - How can I sort object on laravel? 如何在Symfony 2的表单集合中传递当前引用的对象? - How can I pass the current referenced object in form collection in Symfony 2? 如何获得 object 是 Symfony 中的每个响应? - How can I get an object is every response in Symfony? 如何在Symfony 2中的类中获取Request对象? - How can I get Request object inside a class in Symfony 2? 如何阻止Symfony截断我的POSTed对象? - How can I stop Symfony from truncating my POSTed object? 如何转换我的日期 object 以在 Symfony formbuilder 中使用它? - How can I convert my date object to use it in Symfony formbuilder? Symfony2,如何使用KnpPaginatorBundle和QueryBuilder传递参数排序和方向 - Symfony2, How can I pass parameters sort and direction using KnpPaginatorBundle and QueryBuilder PHP:我如何排序和过滤“数组”,即一个Object,实现ArrayAccess? - PHP: how can I sort and filter an “array”, that is an Object, implementing ArrayAccess? 如何在Twig / Symfony2中按对象/表头对表进行排序 - How to sort a table by object/tablehead in Twig/Symfony2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM