1. 程式人生 > >已知陣列 ['a','b','d','a','b','c','b','d'] ,按照出現次數排序後變為['b','a','d'

已知陣列 ['a','b','d','a','b','c','b','d'] ,按照出現次數排序後變為['b','a','d'

public function test_test()
	{
		$arr = ['a','b','d','a','b','c','b','d'];
		$res = [];
		foreach ($arr as $k => $v) {
			$res[$v] = 1;
		}
		var_dump($res);
		for ($i=0; $i < count($arr) ; $i++) { 
			if ($res[$arr[$i]] == 1) {
				for ($j=$i+1; $j < count($arr) ; $j++) { 
					if ($arr[$j] == $arr[$i]) {
						$res[$arr[$i]] += 1;
					}
				}
			}
		}

		arsort($res);
		foreach ($res as $key => $value) {
			$desc[] = $key;
		}

		var_dump($desc);

	}