Hello, guys. Can u please help me with sorting in php? I cant sort out how to sort this thing from a to z. https://github.com/rex2630/codeswholesale-for-woocommerce/blob/master/includes/admin/class-cw-admin-product.php#L92
I tried using sort, but i think i dont understand how array works or idk. Thanks for any help.
It looks like you want to sort the array, but you want to keep the array keys you’re assigning in line 81
.
If that’s the case, use asort
. This function will sort the array in place but keep the product ids correctly lined up:
$list = [1 => 'charlie', 2 => 'able', 3 => 'baker'];
asort($list);
print_r($list);
The above code will produce the output:
Array
(
[2] => able
[3] => baker
[1] => charlie
)
Note that the values are now sorted alphabetically, but the keys remains attached to their original values.
This kind of operation is a hangover of PHP’s slightly weird approach to arrays; most languages will not allow you to sort a list of key => value pairs in this way.
Thank you very much. That’s exactly what i needed