How to rearrange the contents of an array with a field of an object in an array

Source: Internet
Author: User
Tags benchmark
For example, the following is the data I read from the database, and then I want to suppliers_id as a benchmark to rearrange the array, what is the better way?

[    {        cart_id: "6",        suppliers_id: "1",        goods_name: "商品1",    },    {        cart_id: "7",        suppliers_id: "1",        goods_name: "商品2",    }    {        cart_id: "7",        suppliers_id: "1",        goods_name: "商品3",    }]

Become

[    //suppliers_id    1:    [        {            cart_id: "6",            suppliers_id: "1",            goods_name: "商品1",        },        {            cart_id: "7",            suppliers_id: "1",            goods_name: "商品3",        }    ]    //suppliers_id    2:    [        {            cart_id: "7",            suppliers_id: "1",            goods_name: "商品2",        }    ]]

Reply content:

For example, the following is the data I read from the database, and then I want to suppliers_id as a benchmark to rearrange the array, what is the better way?

[    {        cart_id: "6",        suppliers_id: "1",        goods_name: "商品1",    },    {        cart_id: "7",        suppliers_id: "1",        goods_name: "商品2",    }    {        cart_id: "7",        suppliers_id: "1",        goods_name: "商品3",    }]

Become

[    //suppliers_id    1:    [        {            cart_id: "6",            suppliers_id: "1",            goods_name: "商品1",        },        {            cart_id: "7",            suppliers_id: "1",            goods_name: "商品3",        }    ]    //suppliers_id    2:    [        {            cart_id: "7",            suppliers_id: "1",            goods_name: "商品2",        }    ]]

Seeing a label is a php means of powerlessness.

If it is in front-end processing You can use sort(function(a,b){}) this idea.

Using PHP's Uksort function, this shipment 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.