1

I have two arrays: $story and $languages

Now I want to know how many times elements or values in $languages appear in $story array. This means how many times "php", "javascript", "mysql" of $languages array appeared in $story?

How can I do this in php? Can anyone help? Here are my arrays...

$story = array();
$story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
$story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
$story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

$languages = array(
  'php', 'javascript', 'mysql'  
);
1

3 Answers 3

2

I would store the totals in your $languages array. No need to check the count before adding, because adding 0 won't affect your totals.

<?
    $story = array();
    $story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
    $story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
    $story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

    $languages = array(
        'php' => 0,
        'javascript' => 0,
        'mysql' => 0
    );

    foreach ($story as $sk => $sv){
        foreach ($languages as $lk => $lv){
            $languages[$lk] += substr_count($story[$sk], $lk);
        }
    }

    print_r($languages);
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Good answer. If word boundaries or caseless matching is needed, could replace the substr_count part with $languages[$lk] += preg_match_all('/\b'.preg_quote($lk, "/").'\b/i', $story[$sk]);
Hm, but not overlaps, will the regex fix that? I really want to believe the OP won't have any overlaps. Please, please no whammies....
Overlapping was not mentioned by OP also not caseless matching or boundaries, just wanted to mention as a supplement :]
Yeah, I would expect any reasonable "Story" to have mixed case and boundary issues. Overlaps would mean the story was terrible in the first place.
1

With 2 loops: https://ideone.com/grRBeG

$story = array();
$story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
$story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
$story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

$languages = [
    'php' => 0,
    'javascript' => 0,
    'mysql' => 0,
];

for( $i=0,$size=count($story); $i<$size; ++$i )
{
    $text = strtolower($story[$i]);
    foreach( $languages as $word => $nb )
        $languages[$word] += substr_count($text, $word);
}

var_export($languages);

2 Comments

Since this is mostly a copy of my answer, I should reiterate what the comments have said, substr_count takes care of case matching. So that you know, running strtolower in this case only adds processing and serves no purpose.
I won't down vote you or anything, not worth it, keep your 10pts, but others coming to this page should know to avoid this answer, regardless of how tacky it is.
0

Run two foreach loops as shown below.

$finalCount = array();
foreach($story as $current)
{
      $count=0;
     foreach($languages as $language){
         if (substr_count($current,$language) !== false) {
           $count++
         }
     }
     $finalCount[$language] = $count;

}

Comments

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.