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
The first parameter of usort should be an array and $fields
is an object. One option could be to cast it to an array like:
$fields = (array)$fields;
And then pass it to 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;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.