mysterbx Posted February 22, 2008 Share Posted February 22, 2008 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]... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 22, 2008 Share Posted February 22, 2008 <?php $cCount = 0; $i = 0; foreach($myArray as $val){ $sLen = strlen($val); if($sLen > $cCount){ $longestLine = $myArray[$i]; } $i++; } echo "Longest Line: ".$longestLine; ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 22, 2008 Share Posted February 22, 2008 That won't work unless you set $count to the longest line length found so far. Ken Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted February 22, 2008 Share Posted February 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 22, 2008 Share Posted February 22, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 22, 2008 Author Share Posted February 22, 2008 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] => ) ) Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 23, 2008 Author Share Posted February 23, 2008 works with other arrays, but not with this one Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 Right here: $description = trim($description[4][$i]); You are overwriting the array that the script is looping through with a string. Try my example posted above, seeing as it has already been coded as a function as well. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 23, 2008 Author Share Posted February 23, 2008 if i do this: $description = $description[$i]; i get no results... I need to search $description[4]; for the longest array, it workds when array has only 1 result, but it doesnt work when it has more than 1.... Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 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. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 23, 2008 Author Share Posted February 23, 2008 works now... i was doing this: longest_string($description[4]); but I tried: $test = longest_string($description[4]); echo $test; .. and it worked... 10x Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.