<?php $a=array("Cat","Dog","Horse","Dog"); print_r(array_count_values($a)); ?> PHP: it returns Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 ) Code (markup): but i want exact opposite. i have an array like $b = array( [Cat] => 1 [Dog] => 2 [Horse] => 1 ) PHP: but the i have to convert it like array("Cat","Dog","Horse","Dog") Code (markup): how can i do it? any array function ?
Try this: $b = array( "Cat" => 1, "Dog" => 2, "Horse" => 1 ); $result = array(); // will hold the final contents foreach($b as $key => $value) { $t = array(); $t = array_fill(0, $value, $key); // fill the array with a value for the given number of times $result = array_merge((array)$result, (array)$t); // merge this newly created array with the original one } var_dump($result); PHP: Hope this helps