1

I want to search an array with words like "new" "York" "other" "thing", and combine only the ones that make up the words of a state.

In this example, I want the matches for states to be combined in the array:

From this:

<?php 
$states = array("new york", "Nevada");
$names = array("something", "green", "something", "yellow", "new", "york", "new","jersey");

To this:

$names = ( "something", "green", "something", "yellow", "new york", "new jersey");

Hence altering the the names array to combine the states.

6
  • 2
    In your example, how does it know to create New Jersey? If the $states array contained all 50 states, how would it know whether new applied to New York or New Jersey? Commented Jul 18, 2016 at 11:32
  • 1
    Should there be New Jersey in states array? Do i get your logic? Commented Jul 18, 2016 at 11:33
  • Two things need to be clarified here. 1. Should we check the two simultaneous array values only? e.g: "new" and "york. And not "green" and "yellow" since they aren't simultaneous entries? 2. Based on what logic are we getting new york? $states doesn't have new york Commented Jul 18, 2016 at 11:42
  • @ObjectManipulator States does have new york $states = array("new york", "Nevada"); Commented Jul 18, 2016 at 11:50
  • Sorry, I meant new jersey Commented Jul 18, 2016 at 11:51

2 Answers 2

1

Before viewing the solution, take a look at the two assumptions made: 1. Concurrent array elements are checked in $names. 2. Checking is made with respect to $states.

$states = array("new york", "Nevada");
$names  = array("something", "green", "something", "yellow", "new", "york", "new","jersey");
foreach($names as $k => $v) {
    $combined_names = $names[$k]." ".$names[$k+1];
    if (in_array($combined_names, $states)) {
        $names[] = $combined_names;
        $names[$k] = $names[$k+1] = '';
    } 
}
$result = array_values(array_filter($names));

Output:

Array
(
  [0] => something
  [1] => green
  [2] => something
  [3] => yellow
  [4] => new
  [5] => jersey
  [6] => new york
)
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you can solve the "New Jersey" issue in the comments, the following will map one array of words onto a target array (in this case states). The resulting array is then de-duplicated and re-indexed.

If the names need to only match whole words in the target strings, the stripos test can be changed to a more advanced preg_match;

<?php 
$states = array("new york", "Nevada");
$names = array("something", "green", "something", "yellow", "new", "york", "new","jersey");

$result = array_map(function ($e) use ($states) {
    foreach ($states as $state) {
        // The moment a match is found, return it
        if (stripos($state, $e) !== false)
        {
            return $state;
        }
    }

    // If no match was found, fall back to the word in the original array
    return $e;
}, $names);

print_r(array_values(array_unique($result)));

=

Array
(
    [0] => something
    [1] => green
    [2] => yellow
    [3] => new york
    [4] => jersey
)

1 Comment

sorry, no I wanted to just combine anywhere it was a state ... so merge all of the state names if they are next to each other

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.