Jump to content

[SOLVED] finding the biggest needle in haystack


Heylance

Recommended Posts

I know this is probably beginner stuff, but so am I.

 

I have 3 items in an array an I want the one with the longest string length.

 

Example: print_r ($city_list)

outputs:  (Array [0] => Catalina [1] => Catalina Tucson [2] => Tucson)

 

Is there a way to sort by srtlen, small to large as its the largest that I need or if it is on the end

 

That's about it . Thanks

Link to comment
Share on other sites

there maybe an easier way, but my first thought is to recreate the array and use each value's length as its array key.  then you can sort the array by the keys.

 

something like:

<?php

$exisiting_array = array("Catalina", "Catalina Tucson", "Tucson");
$new_array = array();

foreach($exisiting_array as $key => $word){
   $new_key = strlen($word);
   $new_array[$new_key] = $word;
}

ksort($new_array);

echo "<pre>\n";
print_r($new_array);
echo "</pre>";

?>

Link to comment
Share on other sites

Thanks micah1701

 

I'm kinda following what you suggested.

Would that make the new array look like:

([6] => Tucson [8] => Catalina [15] => Catalina Tucson)

The problem is that I don't know what the $word is, only that there is an array.

 

I think I may have been going about what I need to get the wrong way or asked the question the wrong way.

 

This is what I need

 

The array is $city_list

([0] => Catalina [1] => Catalina Tucson [2] => Tucson)

or it could be

([0] => Catalina [1] => Tucson [2] => Tucson Catalina)

 

if($city_list > 1)

$city_name = the value with the longest string length ( which would be "Catalina Tucson" or "Tucson Catalina" )

 

Things to consider (if important):

1. if the array only contains 2 values ($city_list[0] & $city_list[1]), then $city_list[1] will always be the longest string

2. there may be more than 3 values in the array.

 

Thanks.

Link to comment
Share on other sites

With the following $city_name will contain the longest string.

<?php
$l = 0;
foreach($city_list as $city){
  if(strlen($city) > $l){
    //if $city is longer than $l change $l to the length of $city
    //and set $city_name to $city
    $l = strlen($city);
    $city_name = $city;
  }
}
?>

 

EDIT:  What do you want to do if there is more than one city with the same length--and it happens to be the longest length in the list?

Link to comment
Share on other sites

Perfect

Thanks doni49

 

I tried it with 3 and with 4 values in the array and works great.

 

To answer your edit...

 

Sample:

cities table

1. Casa

2. Benson

3. North

4. North Casa

5. North Tucson Casa

6. Starr

7. Tucson

 

input: "North Tucson Casa"

explode input

Get from DB anything that equals the exploded parts or forward combinations of exploded parts

=

Casa, North, North Tucson Casa, Tucson

 

There will always be 1 value with a longer strlen.

 

 

If there is some situation that you think I am missing pertaining to your "edit", please let me know so I can explore it. Thanks again.... Lance

 

---------------

BTW micah1701

 

I figured out what $word is. It is $city_list[0]

and your code worked with the result being what I guessed it would be and it put the longest strlen at the end of the array.  Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.