Home  >  Article  >  Backend Development  >  数组的排序有关问题

数组的排序有关问题

WBOY
WBOYOriginal
2016-06-13 12:04:12921browse

数组的排序问题!
$a=array('张三'=>'肆佰贰拾叁','李四'=>'壹佰玖拾','王五'=>'柒佰','孙六'=>'捌仟叁佰陆拾','陈七'=>'肆万伍仟贰佰');

将这个数组按汉子代表的数字大小进行排序,不改变索引,最后输出的结果应为如下格式:

array('李四'=>'壹佰玖拾','张三'=>'肆佰贰拾叁','王五'=>'柒佰','孙六'=>'捌仟叁佰陆拾','陈七'=>'肆万伍仟贰佰');

代码尽量简单 我是新手,最后排序用数组排序函数
------解决方案--------------------

$a = array(<br />  '张三' => '肆佰贰拾叁',<br />  '李四' => '壹佰玖拾',<br />  '王五' => '柒佰',<br />  '孙六' => '捌仟叁佰陆拾',<br />  '陈七' => '肆万伍仟贰佰'<br />);<br /><br />$t = array_map('cton', $a);<br />array_multisort($t, $a);<br />print_r($a);<br /><br />function cton($s) {<br />  $dw = array('拾' => 10, '佰' => 100, '仟' => 1000, '万' => 10000);<br />  $digital = array('零' => 0, '壹' => 1, '贰' => 2, '叁' => 3, '肆' => 4,<br />                   '伍' => 5, '陆' => 6, '柒' => 7, '捌' => 8, '玖' => 9);<br />  $ar = preg_split('/(' . join('<br><font color='#FF8000'>------解决方案--------------------</font><br>', array_keys($dw)) . ')/',<br />                   $s, -0, PREG_SPLIT_NO_EMPTY <br><font color='#FF8000'>------解决方案--------------------</font><br> PREG_SPLIT_DELIM_CAPTURE);<br />  for($i=0; $i<count($ar); $i+=2) {<br />    $res[] = $digital[$ar[$i]] * (isset($ar[$i+1]) ? $dw[$ar[$i+1]] : 1);<br />  }<br />  return array_sum($res);<br />}
Array<br />(<br />    [李四] => 壹佰玖拾<br />    [张三] => 肆佰贰拾叁<br />    [王五] => 柒佰<br />    [孙六] => 捌仟叁佰陆拾<br />    [陈七] => 肆万伍仟贰佰<br />)<br /><br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn