Jump to content

longest array line


mysterbx

Recommended Posts

Hello,

 

I need to find the longest array line from the [4]th array in my curl generated array..

 

Array looks like this:

Array
(
    [0] => Array
        (
            [0] =>  Synopsis :    Jim proposes to Michelle and she accepts. Now their families are looking forward to it as is Jim's friends, Kevin and Finch. They do their best to keep Stifler from knowing but he does and all he sees is the bachelor party that goes with it. However Jim is more concerned about whether or not Michelle's parents will like him and he is also worried that he can learn how to dance before the wedding.  
   IMDB 
        )

    [1] => Array
        (
            [0] =>  Synopsis 
        )

    [2] => Array
        (
            [0] => 
        )

    [3] => Array
        (
            [0] => :
        )

    [4] => Array
        (
            [0] =>     Jim proposes to Michelle and she accepts. Now their families are looking forward to it as is Jim's friends, Kevin and Finch. They do their best to keep Stifler from knowing but he does and all he sees is the bachelor party that goes with it. However Jim is more concerned about whether or not Michelle's parents will like him and he is also worried that he can learn how to dance before the wedding.  
   IMDB 
        )

    [5] => Array
        (
            [0] => 
        )

)

 

This function should output that 0 is the longest array line ($description[4][0])

 

My code that finds the results

 

preg_match_all("#(synopsis)(\:| \:|\: | \: |)([\w\d\s\,\.\'\"]{30,999})(\n|\t|\r|)#ise", preg_replace ('@<[\/\!]*?[^<>]*?>@si',' ',html_entity_decode($r2)), $description);

 

function should print the longest array from $description[4]...

Link to comment
https://forums.phpfreaks.com/topic/92453-longest-array-line/
Share on other sites

Additionally, your code assumes that the array is numerically indexed.  Seeing as he's using preg_match, it always will be, but in case he needs to adapt it for something else...this should do it:

 

<?php
function longest_string($array_src) {
     $longest_key = false;
     $longest_len = 0;
     foreach ($array_src as $k=>$v)
     {
          $current_len = strlen($v);
          if ($current_len > $longest_len)
          {
               $key = $k;
               $longest_len = $current_len;
          }
     }
     return $key;
}
?>

Will return false if it finds nothing, or the key when it's found.

Link to comment
https://forums.phpfreaks.com/topic/92453-longest-array-line/#findComment-473789
Share on other sites

Right! New code:

<?php
$cCount = 0;
$i = 0;
foreach($myArray as $val){
     $sLen = strlen($val);
     if($sLen > $cCount){
          $longestLine = $myArray[$i];
          $cCount = $sLen;
     }
     $i++;
}
echo "Longest Line: ".$longestLine;
?>

Link to comment
https://forums.phpfreaks.com/topic/92453-longest-array-line/#findComment-473790
Share on other sites

Fatal error: Cannot use string offset as an array in /home/domains/onlineheroes.com/www_root/new.php on line 69

 

Code:

$cCount = 0; $i = 0; foreach($description[4] as $val){ $sLen = strlen($val); if($sLen > $cCount){ $description = trim($description[4][$i]); $cCount = $sLen; } $i++; }
$description = explode("\n", $description);
$description = $description[0];

 

Array:

Array
(
    [0] => Array
        (
            [0] =>  Information  




     
   width
            [1] =>  Information  


Latest News And Announcements
            [2] =>  Synopsis   During 20 years in exile, Turok has become a powerful and feared warrior.  He now returns home to find his village destroyed and his family slaughtered  at the bands of the ruthless tyrant Chichak.  On a mission of vengeance, Turok must journey to the Lost Land, a savage  place forgotten by time, where primeval beats hunt all who enter. Turok  will face his greatest battle as he fights man eating dinosaurs,  merciless cave dwellers, and the darkness inside himself to  take revenge on his sworn enemy. In this epic journey...Turok will find his destiny. In this Lost Land....A legend is born.  IMDb   CODE   http
        )

    [1] => Array
        (
            [0] =>  Info
            [1] =>  Info
            [2] =>  Synopsis 
        )

    [2] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [3] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [4] => Array
        (
            [0] => rmation  




     
   width
            [1] => rmation  


Latest News And Announcements
            [2] =>   During 20 years in exile, Turok has become a powerful and feared warrior.  He now returns home to find his village destroyed and his family slaughtered  at the bands of the ruthless tyrant Chichak.  On a mission of vengeance, Turok must journey to the Lost Land, a savage  place forgotten by time, where primeval beats hunt all who enter. Turok  will face his greatest battle as he fights man eating dinosaurs,  merciless cave dwellers, and the darkness inside himself to  take revenge on his sworn enemy. In this epic journey...Turok will find his destiny. In this Lost Land....A legend is born.  IMDb   CODE   http
        )

    [5] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

)

Link to comment
https://forums.phpfreaks.com/topic/92453-longest-array-line/#findComment-473880
Share on other sites

Change the first $description in that line to something else, such as $str.  You'll also need to change references to it later on.  You're overwriting the array you want to search with the variable $description.  If that doesn't make sense, then once again, take this function:

 

<?php
function longest_string($array_src) {
     $longest_key = false;
     $longest_len = 0;
     foreach ($array_src as $k=>$v)
     {
          $current_len = strlen($v);
          if ($current_len > $longest_len)
          {
               $key = $k;
               $longest_len = $current_len;
          }
     }
     return $key;
}
?>

And call it like longest_string($description[4]) and it will return the key with the longest string.

Link to comment
https://forums.phpfreaks.com/topic/92453-longest-array-line/#findComment-474601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.