Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

i've got this array:

fffarray(2) {
  [0]=>
  array(1) {
    ["keyword1"]=>
    string(17) "Software Engineer"
  }
  [3]=>
  array(2) {
    ["keyword1"]=>
    string(10) "Hampelmann"
    ["keyword2"]=>
    string(17) "Software Engineer"
  }
}

i want an output like

fffarray(1) {
  ["Software Engineer"]=>
  int(2)
  ["Hampelmann"]=>
  int(1)
}

I really tried my best but can't achieve this.

May you please help me.

Thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

if your array is always going to have only two dimensions, you might use this:

$finalArray = array();

foreach ($fffarray as $array)
 {foreach ($array as $key => $string)
   {$finalArray[$string] = $finalArray[$string] + 1;}}

this will count how many occurences of each value in all subarrays

share|improve this answer
    
works, many thanks –  user3169083 Feb 5 '14 at 18:55
    
Dang, beat me by a little bit. :) –  Biotox Feb 5 '14 at 18:59

Try building a new array that keeps count like this:

$newArray = array(
  array("Software Engineer"),
  array("Hampelmann", "Software Engineer")
);

$countArray = array();

foreach($newArray as $tempArray){
  foreach($tempArray as $key => $value){
    if(array_key_exists($value, $countArray)){
      $countArray[$value] = $countArray[$value] + 1;
    } else {
      $countArray[$value] = 1;
    }
  }
}

echo '<pre>'.print_r($countArray, true).'</pre>';

The output comes out like:

Array
(
  [Software Engineer] => 2
  [Hampelmann] => 1
)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.