therealbigpepe
therealbigpepe

Reputation: 1549

How Can I Extract the stringlength higher word in an array?

In the first time, I'm sorry for my disastrous english. After three days trying to solve this, i give up.

Giving this array:

$names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" )

I have to extract the full name of the higher first stringlength word of each full name. In this case, considering this condition Benjamin will be the higher name. Once this name extracted, I have to print the full name.

Any ideas ? Thanks

Upvotes: 0

Views: 45

Answers (4)

John WH Smith
John WH Smith

Reputation: 2773

Allow me to provide a handmade solution.

<?php
$maxLength = 0;
$fullName = "";
$parts = array();

foreach($names as $name){
    /* Exploding the name into parts.
     * For instance, "James Walter Case" results in the following array :
     *     array("James", "Walter", "Case") */
    $parts = explode(' ', $name);
    if(strlen($parts[0]) > $maxLength){ // Compare first length to the maximum.
        $maxLength = strlen($parts[0]);
        $fullName = $name;
    }
}

echo "Longest first name belongs to " . $fullName . " (" . $maxLength . " character(s))";
?>

Basically, we iterate over the array, split each name into parts (delimited by spaces), and try to find the maximum length among first parts of names ($parts[0]). This is basically a maximum search algorithm.

There may be better ways with PHP array_* functions, but well, this one gets pretty self-explanatory.

Upvotes: 0

hlscalon
hlscalon

Reputation: 7552

As said before, you can use array_map to get all the lenghts of the first strings, and then get the name based on the key of that value.

$names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" );

$x = array_map(
    function ($a) {
        $x = explode (" ", $a); // split the string
        return strlen($x[0]); // get the first name
    }
, $names);

$maxs = array_keys($x, max($x)); // get the max values (may have equals)
echo $names[$maxs[0]]; // get the first max

-- EDIT

Actually, there is a better way of doing this, considering this case. You can simply order the array by the lenght of the first names, and then get the first key:

usort($names, 
    function ($a, $b) {
        $aa = explode(" ", $a); // split full name
        $bb = explode(" ", $b); // split full name
        if (strlen($aa[0]) > strlen($bb[0])){
            return 0; // if a > b, return 0
        } else {
            return 1; // else return 1
        }
    }
);
echo $names[0]; // get top element

Upvotes: 1

Christian Bonato
Christian Bonato

Reputation: 1306

How about this?

    $names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" );

    if (count($names) > 0)
    {
        $higher_score = 0;

        foreach ($names as $name_key => $name_value) 
        {
            $name_array = explode(" ", $name_value);
            // echo("<br><b>name_array -></b><pre><font FACE='arial' size='-1'>"); print_r($name_array); echo("</font></pre><br>");

            $first_name = $name_array[0];
            // echo "first_name -> ".$first_name."<br>";

            $score = strlen($first_name);
            // echo "score -> ".$score."<br>";

            if ($score > $higher_score)
            {
                $higher_score = $score;
                $winner_key = $name_key;                }
        }
        reset($names);
    }

echo("longest first name is ".$names[$winner_key]." with ".$higher_score." letters.");

Upvotes: 1

J A
J A

Reputation: 1766

You can try something like..

$maxWordLength=0;
$maxWordIndex=null;
foreach ($names as $index=>$name){
   $firstName=first(explode(' ',$name)); 
   if (strlen($firstName)>maxWordLength){
      $maxWordLength=strlen($firstName);
      $maxWordIndex=$index; 
   }
}

if ($maxWordIndex){
   echo $names[$maxWordIndex];
}

Upvotes: 0

Related Questions