PHP array sorting

Source: Internet
Author: User
There are two Arrays: {code ...} is there any built-in function in php that can sort $ array1 by $ array2 in the following format: {code ...} I have read the array_multisort () documentation, but it does not seem to support it. There are two Arrays:

$ Aray1 = ('key3' => 'value3 ', 'key2' => 'value2', 'key1' => 'value1); $ aray2 = ('key1 ', 'key3', 'key2'); // The sequence is not 123.

Is there any built-in function in php that can sort $ array1 by $ array2 in the following format:

$aray1 = (    'key1' => 'value1',    'key3' => 'value3',    'key2' => 'value2);

I have read the array_multisort () documentation, but it does not seem to support it.

Reply content:

There are two Arrays:

$ Aray1 = ('key3' => 'value3 ', 'key2' => 'value2', 'key1' => 'value1); $ aray2 = ('key1 ', 'key3', 'key2'); // The sequence is not 123.

Is there any built-in function in php that can sort $ array1 by $ array2 in the following format:

$aray1 = (    'key1' => 'value1',    'key3' => 'value3',    'key2' => 'value2);

I have read the array_multisort () documentation, but it does not seem to support it.

It seems that no. array_multisort () mainly sorts multi-dimensional arrays.

Use the User-Defined sorting function usort (), but you still need to write it yourself.

foreach($array2 as $i => $key) {    $result[$key] = $array1[$key];}var_dump($result);

$data = array();array_walk($aray2,function($value, $key) use($aray1, &$data){    $data[$value] = $aray1[$value];    });print_r($data);

Based on my experience, there is no such internal function

You can write a method by yourself.

You can write a method by yourself.

Write it yourself

Only write one according to the sorting rules.

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.