Answer the question
In order to leave comments, you need to log in
How to sort objects (alphabetical order) in a query by value?
There is a multidimensional array where you need to sort the values alphabetically.
The array was uploaded to pastebin, it does not fit here.
shopColorValue object should be alphabetized by field value shopColorValuevalue => 'color'
I was able to reduce a multidimensional array into a normal one:
foreach ( $filters as $filter ) {
//arsort($filter);
foreach ( $filter as $key => $value ) {
wa_dump($filter);
}
}
Array
(
id => '675'
parent_id => NULL
code => 'tsvet1'
status => 'public'
name => 'Цвет'
type => 'color'
selectable => '1'
multiple => '1'
count => '25'
values => Array
(
27 => shopColorValue object
{
shopColorValuecode => '0'
shopColorValuevalue => 'чёрный'
shopColorValueid => '27'
shopColorValuesort => '1'
shopColorValue_data => NULL
feature_id => '675'
}
34 => shopColorValue object
{
shopColorValuecode => '255'
shopColorValuevalue => 'синий'
shopColorValueid => '34'
shopColorValuesort => '9'
shopColorValue_data => NULL
feature_id => '675'
}
)
)
public function sortirovka_filters($filters) {
function cmp_obj($a, $b):bool {
$al = strtolower($a->shopColorValuevalue);
$bl = strtolower($b->shopColorValuevalue);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}
uasort($filters['675']['values'], "cmp_obj");
return $filters;
}
Answer the question
In order to leave comments, you need to log in
usort
php - sorting an array of objects by object fields
Sorting objects in PHP 7
Sorting objects in PHP
An example for your data:
usort($array['675']['values'], function($a, $b) {
return $a->color <=> $b->color;
});
uasort($array['675']['values'], function($a, $b) {
return $a->color <=> $b->color;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question