Posted inTechnology / php / Programming

Sort associative array by specific key in PHP

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.

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

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

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 Pakainfo.com 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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype