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