How to rearrange the array content based on a field of an object in an array

Source: Internet
Author: User
For example, below is the data I read from the database, and then I want to rearrange this Array Based on suppliers_id. What is a better method? {Code ...} changed to {code ...} for example, below is the data I read from the database, and then I want to rearrange this Array Based on suppliers_id. What is a better method?

[{Cart_id: "6", suppliers_id: "1", goods_name: "commodity 1" ,}, {cart_id: "7", suppliers_id: "1", goods_name: "product 2" ,}{ cart_id: "7", suppliers_id: "1", goods_name: "Product 3",}]

Change

[// Suppliers_id 1: [{cart_id: "6", suppliers_id: "1", goods_name: "item 1" ,}, {cart_id: "7", suppliers_id: "1", goods_name: "commodity 3",}] // suppliers_id 2: [{cart_id: "7", suppliers_id: "1", goods_name: "product 2",}]

Reply content:

For example, below is the data I read from the database, and then I want to rearrange this Array Based on suppliers_id. What is a better method?

[{Cart_id: "6", suppliers_id: "1", goods_name: "commodity 1" ,}, {cart_id: "7", suppliers_id: "1", goods_name: "product 2" ,}{ cart_id: "7", suppliers_id: "1", goods_name: "Product 3",}]

Change

[// Suppliers_id 1: [{cart_id: "6", suppliers_id: "1", goods_name: "item 1" ,}, {cart_id: "7", suppliers_id: "1", goods_name: "commodity 3",}] // suppliers_id 2: [{cart_id: "7", suppliers_id: "1", goods_name: "product 2",}]

The tag isphpIt indicates powerless.

You can usesort(function(a,b){})This approach comes.

The uksort function of PHP can receive two parameters.

$arr = [{c:"213"},{c:"dadad"}]function compare_func($a, $b){    return $a=>c - $b=>c;}uksrot($arr,"compare_func" );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.