Summary of thinkphp Development (III.)--two-D array add key value, sort

Source: Internet
Author: User

To achieve the requirements:
If the data obtained in the database is:

$pos =[{' word ' => ' vocabulary 1 ', ' value ' => ' weight value 1 '},{' word ' => ' words 2 ', ' value ' => ' weights 2 '},{' word ' => ' words 3 ', ' Value ' => ' Weight value 3 '}];

$neg =[{' word ' => ' vocabulary 4 ', ' Weight value 4 '},{' word ' => ' vocabulary 5 ', ' Value ' => ' right value 5 '},{' word ' => ' vocabulary 6 ', ' Value ' => ' weight value 6 '}];

The interface outputs data in the following form:

$word =[{' word ' => ' vocabulary 1 ', ' value ' => ' weight value 1 ', ' emotion ' =>1},{' word ' => ' vocabulary 2 ', ' value ' => ' weight 2 ', ' emotion ' => 1},{' word ' => ' vocabulary 3 ', ' value ' => ' weight value 3 ', ' emotion ' =>1},{' word ' => ' vocabulary 4 ', ' Value ' => ' weight 4 ', ' Emotion ' =>-1}, {' word ' => ' vocabulary 5 ', ' Value ' => ' weight 5 ', ' emotion ' =>-1},{' word ' => ' vocabulary 6 ', ' Value ' => ' weight 6 ', ' emotion ' =>-1}];

It can be seen that the emotion value of POS positive emotional vocabulary is 1,neg-1.
Specific implementation code:

function Emotion_word ($posWords, $negWords) {function Addkey (& $val, $key, $param) {
    $val [$param [' key ']] = $param [' Val '];  Array_walk ($posWords, ' Addkey ', Array (' key ' => ' emotion ', ' Val ' =>1));  The array adds the key value Array_multisort (Array_column ($posWords, ' value '), Sort_desc, $posWords);  Arrays are sorted according to value from large to small array_walk ($negWords, ' Addkey ', Array (' key ' => ' emotion ', ' Val ' =>-1));  The array adds the key value Array_multisort (Array_column ($negWords, ' value '), Sort_desc, $negWords); The array is sorted according to value from big to small return array_merge_recursive ($posWords, $negWords); Array Merge} 

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.