PHP implode array and list results with some logic

Hello,

I am working with an array and would like to retrieve a list of results from the strings.  This is just test data, I am doing this to learn more about arrays and would like to return results based on the logic below (//logic in comments).

Thank you

Data:
"The Cat ran with the hat."
"The bat had a cat."
"The rat sat on the Bat!"

Results after logic:
"bat"
"cat"
"hat"
"ran"
"rat"
"sat"

<?php

$myArr = Array("The Cat ran with the hat.",
"the bat had a cat.",
"The rat sat on the Bat!");

//implode $myArr

//for each $myArr

//remove any unwanted strings
//not sure what is better preg_replace or str_replace.  If no regEx, I've read that string replace is better?
//unwanted strings - regardless of upper/lower case
$not_these_str("The",
" with ",
" had ",
" a ",
" on ",
"!",
"."
)


//$results = remove any duplicates from results

//$results = arrange alphanumeric

echo $results;

?>

//trying for these as results:

"bat"
"cat"
"hat"
"ran"
"rat"
"sat"
johnsonallstarsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Please see: http://iconoun.com/demo/temp_johnsonallstars.php
<?php // demo/temp_johnsonallstars.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28676605.html
 */
error_reporting(E_ALL);
echo '<pre>';

// AGGREGATE THE RESULTS IN THIS ARRAY
$m = [];

// THE TEST DATA
$data = <<<EOD
"The Cat ran with the hat."
"The bat had a cat."
"The rat sat on the Bat!"
EOD;

// THE ARRAY OF STOPWORDS
$not_these =
[ "the"
, "with"
, "had"
, "a"
, "on"
]
;
// NORMALIZE THE DATA
$data = strtolower($data);
$data = preg_replace('/[^a-z ]/', ' ', $data);
$data = preg_replace('/\s+/', ' ', $data);
$data = trim($data);
$data = explode(' ', $data);

// ACTIVATE THIS TO CHECK THE NORMALIZATION
// var_dump($data);

// REMOVE STOPWORDS
foreach ($data as $key => $str)
{
    if (in_array($str, $not_these)) unset($data[$key]);
}

// KEEP ONLY THE SORTED LIST OF WORDS
$data = array_unique($data);
sort($data);
print_r($data);

Open in new window

johnsonallstarsAuthor Commented:
Hi Ray..

Your result worked great for me.  How would it differ in an array vs. the <<<EOD. syntax that you have?

As the EOD syntax  it is working, but when I set to an array.. I get this error

strtolower() expects parameter 1 to be string, array given ….on line 28

$data = array(
"The Cat ran with the hat.",
"The bat had a cat.",
"The rat sat on the Bat!"
);
Ray PaseurCommented:
The "EOD" notation is used to define a string data type.  It's called HEREDOC notation.
https://php.net/language.types.string

The explode() function is used to convert a string into an array.
https://php.net/manual/en/function.explode.php

So if you were starting with a collection of arrays, you might use implode() to turn the collection into one long string and perform the operations on the string data.
https://php.net/manual/en/function.implode.php

In other words, 7+2 = 9 and 5+4 = 9, just different ways to get to the same result when you start from different data points.

Also, I had an unused bit of code in the original snippet, so this is more on point.
<?php // demo/temp_johnsonallstars.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28676605.html
 */
error_reporting(E_ALL);
echo '<pre>';

// THE TEST DATA
$data = <<<EOD
"The Cat ran with the hat."
"The bat had a cat."
"The rat sat on the Bat!"
EOD;

// THE ARRAY OF STOPWORDS
$not_these =
[ "the"
, "with"
, "had"
, "a"
, "on"
]
;
// NORMALIZE THE DATA
$data = strtolower($data);
$data = preg_replace('/[^a-z ]/', ' ', $data);
$data = preg_replace('/\s+/', ' ', $data);
$data = trim($data);
$data = explode(' ', $data);

// ACTIVATE THIS TO CHECK THE NORMALIZATION
// var_dump($data);

// REMOVE STOPWORDS
foreach ($data as $key => $str)
{
    if (in_array($str, $not_these)) unset($data[$key]);
}

// KEEP ONLY THE SORTED LIST OF WORDS
$data = array_unique($data);
sort($data);
print_r($data);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnsonallstarsAuthor Commented:
Very nice.  Ok..   Yes, worked both ways!

Thanks again!!
Ray PaseurCommented:
Thanks for the points and thanks for using E-E!  Best regards, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.