Sort array by value alphabetically php
You want the php function "asort":
http://php.net/manual/en/function.asort.php
it sorts the array, maintaining the index associations.
Edit: I've just noticed you're using a standard array (non-associative). if you're not fussed about preserving index associations, use sort():
http://php.net/manual/en/function.sort.php
PHP sort array alphabetically using a subarray value
Here is your answer and it works 100%, I've tested it.
<?php
$a = Array(
1 => Array(
'name' => 'Peter',
'age' => 17
),
0 => Array(
'name' => 'Nina',
'age' => 21
),
2 => Array(
'name' => 'Bill',
'age' => 15
),
);
function compareByName($a, $b) {
return strcmp($a["name"], $b["name"]);
}
usort($a, 'compareByName');
/* The next line is used for debugging, comment or delete it after testing */
print_r($a);
PHP: How to sort values of an array in alphabetical order?
See
natcasesort
: http://www.php.net/manual/en/function.natcasesort.phpusort
: http://www.php.net/manual/en/function.usort.php with a comparator function that comparesstrtolower(a)
andstrtolower(b)
.
sort array by length and then alphabetically
You can put both of the conditions into a usort
comparison function.
usort($array, function($a, $b) {
return strlen($a) - strlen($b) ?: strcmp($a, $b);
});
The general strategy for sorting by multiple conditions is to write comparison expressions for each of the conditions that returns the appropriate return type of the comparison function (an integer, positive, negative, or zero depending on the result of the comparison), and evaluate them in order of your desired sort order, e.g. first length, then alphabetical.
If an expression evaluates to zero, then the two items are equal in terms of that comparison, and the next expression should be evaluated. If not, then the value of that expression can be returned as the value of the comparison function.
The other answer here appears to be implying that this comparison function does not return an integer greater than, less than, or equal to zero. It does.
Sorting an array of arrays alphabetically by value in PHP
$sorted = array();
foreach ($currency_list as $currency => $countries) {
foreach ($countries as $country) {
$sorted[$country] = $currency;
}
}
ksort($sorted);
print_r($sorted); // do your loop here
Alphabetically sort multidimensional array's keys and values recursively in PHP
you need to check keys are numeric or alphabetic. try below solution you may need to modify conditions for your purpose:
<?php
function isAssoc(array $arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
function sortArray(&$arr){
if(isAssoc($arr)){
ksort($arr);
} else{
asort($arr);
}
foreach ($arr as &$a){
if(is_array($a)){
sortArray($a);
}
}
}
$unsorted = array(
'D' => array(
'C' => array('c', 'b', 'a'),
'B' => 'bvalue',
'A' => array('a', 'c', 'b'),
),
'C' => 'cvalue',
'B' => 'bvalue',
'A' => array(
'Z' => 'zvalue',
'A' => 'avalue',
'B' => 'bvalue',
)
);
sortArray($unsorted);
print_r($unsorted);
Output
Array
(
[A] => Array
(
[A] => avalue
[B] => bvalue
[Z] => zvalue
)
[B] => bvalue
[C] => cvalue
[D] => Array
(
[A] => Array
(
[0] => a
[2] => b
[1] => c
)
[B] => bvalue
[C] => Array
(
[2] => a
[1] => b
[0] => c
)
)
)
Sort multidimensional array alphabetically, by value and store it in a new variable
1) Store the inital array in a new variable:
$atoz_people = $people;
2) Create sorting function:
function sort_by_name($a,$b)
{
return $a["name"] > $b["name"];
}
3) Sort array
uasort($atoz_people,"sort_by_name");
print_r_html($atoz_people);
@B. Desai: many thanks!
Sort array alphabetically in PHP
So what solve my problem is @LeoTahk 's suggestion of adding ksort().
Final code:
<div class="col-lg-3" align="center">
<?php
$fields = get_field_objects();
//working solution
ksort($fields);
if($fields):
....
endif; ?>
</div>
Related Topics
Call Parent Constructor Before Child Constructor in PHP
Laravel Input Facade VS Request Facade
Phpmailer Attachment, Doing It Without a Physical File
Executing Git Commands via PHP
Php: Variable-Length Argument List by Reference
What Is the Best PHP Dom 2 Array Function
Download Multiple Files as a Zip Folder Using PHP
Phpmailer Send Gmail Smtp Timeout
What Is the Easiest Way to Use the Head Command of Http in PHP
Php: What If I Call a Static Method in Non-Static Way
Laravel 5.3 - How to Add Sessions to 'Api' Without Csrf
How to Upload Image PHP and Insert Path in MySQL
How to Use MySQL Found_Rows() in PHP
Pdo Cannot Execute Queries While Other Unbuffered Queries Are Active
Regular Expression for Checking Website Url
Given an Email as Raw Text, How to Send It Using PHP