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

Email
2,4353 gold badges36 silver badges64 bronze badges
asked Feb 5, 2014 at 18:40

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

answered Feb 5, 2014 at 18:49
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
)
answered Feb 5, 2014 at 18:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.