Viewed   93 times

This function returns NULL while $alias having value in second recursion. In first call it return the required value but when first if not matched and it recurse first the required value in avilable in $alias variable but it does not return anything.

public function checkAlias($fname='',$lname=''){

        if(!empty($fname)){
        $fname = mysql_real_escape_string($fname);
        }
        if(!empty($lname)){
        $lname = mysql_real_escape_string($lname);
        }

    $alias = strtolower($fname).strtolower($lname);
    $sql = "Select ALIAS from table where ALIAS = '$alias'";
    $query = mysql_query($sql);
    $row = mysql_fetch_row($query);
    $string_length = strlen($alias) - 1;
    $result_string = substr($alias,0,$string_length);

    if(!$row){
            print $alias;   // is printing value 
        return $alias;  // but here it returns null
    }else{
        $this->checkAlias($result_string);
        } 
    }

 Answers

5

You forgot to return the result of the recursion call:

return $this->checkAlias($result_string);
Tuesday, October 25, 2022
 
4

Is a global variable necessary? Otherwise you could simplify it this way:

function getResp($objectPassed, &$in_arr = array()) {  // <-- note the reference '&'

    foreach($objectPassed as $element) {
        if(is_object($element) || is_array($element)) { // <-- else if statement simplified
            getResp($element,$in_arr);
        } else {
            // XML is being passed, need to strip it
            $element = strip_tags($element); 

            // Trim whitespace
            $element = trim($element);

            // Push to array  
            if($element != ''  &&                      // <-- everything in one test
              !preg_match("/^[0-9]$/", $element) &&
              !in_array($element,$in_arr)) 
            {                    
                  $in_arr[] = $element;  
            } 
        }
    }
    return $in_arr;
}

Then you do:

$result = getResp($data);

If a recursive function has to access the same resource over and over again (in this case the initial array), I would always pass this as a reference.
I don't know if is measurable but I would guess that this is much more efficient than copying values.

Thursday, December 15, 2022
 
rusalex
 
2

You need to return the recursive function when you recurse, otherwise it will return nothing.

if($result['parent_id'] != '0') {

   return $this->Recursive($result['parent_id'],$category_array) ;

 } 
Thursday, August 18, 2022
 
btpka3
 
3

Here's a simple algo. Iterate from 1 to 2count(array)-1. On each iteration, if j-th bit in a binary representation of the loop counter is equal to 1, include j-th element in a combination.

As PHP needs to be able to calculate 2count(array) as an integer, this may never exceed PHP_INT_MAX. On a 64-bit PHP installation your array cannot have more than 62 elements, as 262 stays below PHP_INT_MAX while 263 exceeds it.

EDIT: This computes all possible combinations, not permutations (ie, 'abc' = 'cba'). It does so by representing the original array in binary and "counting up" from 0 to the binary representation of the full array, effectively building a list of every possible unique combination.

$a = array('a', 'b', 'c', 'd');

$len  = count($a);
$list = array();

for($i = 1; $i < (1 << $len); $i++) {
    $c = '';
    for($j = 0; $j < $len; $j++)
        if($i & (1 << $j))
            $c .= $a[$j];
    $list[] = $c;
}

print_r($list);
Tuesday, November 22, 2022
 
4

You're not returning the text properly e.g.

    } else {
        echo 'else 1<br />';
        return $str;   <---nothing in the 'parent' caller catches this, so it's lost
    }

Anywhere you do recursion and need to return a value, you must capture/return the recursive call itself:

    return check_length(substr($str, 0, -1), $max, $size, true);

or

    $newstr = check_length(...);
    return $newstr;
Tuesday, November 8, 2022
 
abranhe
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :