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

Sign up to request clarification or add additional context in comments.

1 Comment

Dang, beat me by a little bit. :)
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
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.