lundi 23 avril 2018

Find the longest and second longest string from a one dimensional array in PHP

Can anyone help? I am trying to print out the longest string and second longest string in php

function findLongestStringFromArray($array = array()) {

if(!empty($array)){
    $length_of_arr = array_map('strlen', $array); 
    $maxLength = max($length_of_arr); // find highest value in the array
    $key = array_search($maxLength, $length_of_arr);
    $key2 = array_search(next($maxLength), $length_of_arr);
    return array('length' => $maxLength,'postion' => $key,'longest- 
    string'=>$array[$key], 'secondLongest-string'=>$array[$key2]);
      }
     }
    $arrData = array("car", "paper", "pencil", "football", "databases");
    print_r(findLongestStringFromArray($arrData));

And then i get this error message Warning: next() expects parameter 1 to be array, integer given in C:\xampp\htdocs\php\lab2.php on line 134 Array ( [length] => 13 [postion] => 4 [longest- string] => databases [secondLongest-string] => car)




Aucun commentaire:

Enregistrer un commentaire