1

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

2 Answers 2

2

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

1
  • Dang, beat me by a little bit. :) Commented Feb 5, 2014 at 18:59
1

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
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.