Sort associative array by specific key in PHP – Technology

Sort associative array by specific key in PHP – Technology

In this post we will give you information about Sort associative array by specific key in PHP – Technology. Hear we will give you detail about Sort associative array by specific key in PHP – TechnologyAnd how to use it also give you demo for it if it is necessary.

Today, We want to share with you Sort associative array by specific key in PHP.In this post we will show you php sort array by specific key, hear for sort array in php without using function we will give you demo and example for implement.In this post, we will learn about Sorting an associative array by a specific key with PHP with an example.

Sort associative array by specific key in PHP

There are the Following The simple About Sort associative array by specific key in PHP Full Information With Example and source code.

Another must read:  OrderBy Filter using Angular Example

As I will cover this Post with live Working example to develop array sorting php, associative array,, so the sort array in php, sort associative array for this example is following below.

When it comes to Complex task an array, PHP has a lot to do and Nice Exp. While current working on project very major task where we some times stuck is an PHP array sorting.

sort array in php without using function

here, Lets start to Create PHP sort associative array by key in ascending order Example below.

//Sort associative array by specific key in PHP
$my_lang_arr[] = [
	'title' => 'Magento',
	'order' => 3,
];

$my_lang_arr[] = [
	'title' => 'Vuejs',
	'order' => 2,
];

$my_lang_arr[] = [
	'title' => 'Laravel',
	'order' => 1,
];
usort($my_lang_arr, function ($a, $b) {
	return $a['title'] <=> $b['title'];
});
print_r($my_lang_arr);

Example 2: php sort array by specific key

$array = array(
    array('name' => 'Vuejs',    'book_price' => 200),
    array('name' => 'Laravel', 'book_price' => 145),
    array('name' => 'Angularjs', 'book_price' => 160),
    array('name' => 'Magento',  'book_price' => 120),
    array('name' => 'Nodejs',  'book_price' => 135),
);

Sorting an associative array by a specific key with PHP

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

function sortByName($a, $b)
{
    $a = $a['name'];
    $b = $b['name'];

    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}

function sortByWeight($a, $b)
{
    $a = $a['book_price'];
    $b = $b['book_price'];

    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}


Call PHP Function

usort($array, 'sortByName');
usort($array, 'sortByWeight');

Sort associative array in php

function sortBy($columnm, &$array, $sort_direction = 'asc')
{
    usort($array, create_function('$a, $b', '
        $a = $a["' . $columnm . '"];
        $b = $b["' . $columnm . '"];

        if ($a == $b) return 0;

        $sort_direction = strtolower(trim($sort_direction));

        return ($a ' . ($sort_direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1;
    '));

    return true;
}

sort array in php using function

Another must read:  Laravel Global variable Sharing Data with all views

sortBy('name',   $array);
sortBy('book_price', $array);

PHP Simple sort in the opposite order

sortBy('name',   $array, 'desc');
sortBy('book_price', $array, 'desc');
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Sort associative array by specific key in PHP.
I would like to have feedback on my onlinecode blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Hope this code and post will helped you for implement Sort associative array by specific key in PHP – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  59  =  63

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US