1

Hi I am trying to find the longest string in the following array:

$niza = array( array(5, 1.15 , 15),
array('unknown', 0.75 , 5, array(2, 'bla', 1.15) ),
array(array('text'), 1.15 , 7)

I am using the following function but I dont get the desired result, the value of $len remains unchanged. Can you help me?

function getLongest($array){
$longest="";
$len=0;

function arrWalk($item){
    global $len;
    global $longest;
    if(is_string($item)) {
        echo $len . "<br>";
        if (strlen($item) > $len) {
            $longest = $item;
            $len = strlen($item);
        }
    }
}

array_walk_recursive($array,'arrWalk');

echo $len;

}

arrWalk($niza);
2
  • I suppose array_reduce() would be more appropriate Commented Nov 5, 2016 at 11:17
  • I think getLongest($arr) is never being called. Commented Nov 5, 2016 at 11:21

3 Answers 3

2

To get longest string element from array you can use below the recursive function.

$niza = array(
    array(5, 1.15 , 15),
    array(
        'unknown', 0.75 , 5,
        array(2, 'bla', 1.15)
    ),
    array(array('text'), 1.15 , 7)
);

$longestString = '';
function getLongestString( $param )
{
    global $longestString;

    if ( is_array($param) )
    {
        foreach ($param as $val)
        {
            if ( is_string($val) && strlen($val) > strlen($longestString) )
            {
                $longestString = $val;
            }
            else
            {
                getLongestString( $val );
            }
        }
    }
    elseif ( is_string($param) && strlen($param) > strlen($longestString) )
    {
        $longestString = $param;
    }
}

getLongestString( $niza );

print $longestString;
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the answer!

function array_flatten($array) {
if (!is_array($array)) {
    return FALSE;
}
$result = array();
$i=0;
foreach ($array as $item) {
    if (is_array($item)) {
        $result = array_merge($result, array_flatten($item));
    }
    else if(is_string($item)) {
        array_push($result,$item);
    }
}
return $result;
}


function getLongest($array){
$arr_nivo_1 =array_flatten($array);

$longest="";
$maxLen=0;
foreach ($arr_nivo_1 as $item){
    if(strlen($item)>$maxLen){
        $maxLen=strlen($item);
        $longest=$item;
    }
}
return $longest;


}

1 Comment

And I call getLongest() with the array I have defined :D
0

You can simply go with below code that will help to get your expected result

<?php


$niza = array(array(5, 1.15 , 15), array('unknown', 0.75 , 5, array(2, 'bla', 1.15) ), array(array('text'), 1.15 , 7));

$crew = '0'; // assign by default one value that count will be default 1

function getLongest($item, $key)
{
     global $crew;
     if(strlen($item)  >= strlen((string)$crew)){
    $crew = $item;
     }
}

array_walk_recursive($niza, 'getLongest');

echo $crew;
?>

You should take global variable to store lateste value in the variable at recursive state

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.