Why are print_r and return returning different values?

Sinister Beard

I have the following custom function in PHP / Wordpress.

function GetAncestors($post_id, $ancestors = array()) {

    $query = 'SELECT `wp_terms`.term_id, `wp_terms`.name, `wp_term_taxonomy`.parent FROM `wp_terms` LEFT JOIN `wp_term_taxonomy` ON `wp_terms`.term_id = `wp_term_taxonomy`.term_id WHERE `wp_terms`.term_id = '.$post_id;
    $term_data = RunQuery($query)[0];
    array_push($ancestors,$term_data);
    if($term_data[parent]!='11') GetAncestors($term_data[parent],$ancestors);

    else print_r($ancestors);
    //else return $ancestors;

}

If I print_r the array, it returns the expected result. If I return the value of the array and print_r it outside of the function (which is what I want to do), it returns a blank string.

Results from print_r:

Array ( [0] => Array ( [term_id] => 95 [name] => PDR (Appraisals) [parent] => 91 ) [1] => Array ( [term_id] => 91 [name] => Your career, learning and development [parent] => 14 ) [2] => Array ( [term_id] => 14 [name] => You At ... [parent] => 11 ) ) 

Why is this?

Flosculus

Shouldn't it be like this:

// Changed ancestors to reference
// Changed constant 'parent' to string
function GetAncestors($post_id, &$ancestors = array()) {

    $query = 'SELECT `wp_terms`.term_id, `wp_terms`.name, `wp_term_taxonomy`.parent FROM `wp_terms` LEFT JOIN `wp_term_taxonomy` ON `wp_terms`.term_id = `wp_term_taxonomy`.term_id WHERE `wp_terms`.term_id = '.$post_id;
    $term_data = RunQuery($query)[0];
    array_push($ancestors,$term_data);
    if($term_data['parent']!='11') {
        GetAncestors($term_data['parent'],$ancestors);
    }
}

$ancestors = array();
GetAncestors($id, $ancestors);

print_r($ancestors);

Personally I'd write it like this for utility:

function GetAncestors($post_id, &$ancestors = null) {
    if (null === $ancestors) {
        $ancestors = array();
    }

    $query  = 'SELECT `wp_terms`.term_id, `wp_terms`.name, `wp_term_taxonomy`.parent FROM `wp_terms` LEFT JOIN `wp_term_taxonomy` ON `wp_terms`.term_id = `wp_term_taxonomy`.term_id WHERE `wp_terms`.term_id = '.$post_id;
    $result = RunQuery($query);

    if (count($result) > 0) {
        $count = 1;
        $term_data = $result[0];
        array_push($ancestors,$term_data);

        if($term_data['parent']!='11') {
            $count += GetAncestors($term_data['parent'],$ancestors);
        }

        return $count;
    }

    return 0;
}

if (GetAncestors($id, $ancestors) > 0) {
    print_r($ancestors);
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

print_r return 1 althought no record was saved

분류에서Dev

Why is AES function returning different value?

분류에서Dev

Handle different kinds of return values in a single decorator

분류에서Dev

variable is returning two values, how to store them in different variable?

분류에서Dev

echo and (var_dump or print_r) showing completely different things on laravel object

분류에서Dev

Why is `_.map` returning undefined when I return `false`?

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

print_r return 1 비록 기록이 저장되지 않았지만

분류에서Dev

Why does the MBR return a different offset than Ubuntu's commands?

분류에서Dev

Why does this code return undefined 4 times instead of the array values?

분류에서Dev

Why MySQL BIT_OR() is returning different value than PHP bitwise operation

분류에서Dev

Why MySQL BIT_OR() is returning different value than PHP bitwise operation

분류에서Dev

C printf() not returning values

분류에서Dev

print_r과 return이 다른 값을 반환하는 이유는 무엇입니까?

분류에서Dev

Why is this not returning an @@ERROR?

분류에서Dev

trig functions returning incorrect values

분류에서Dev

MySQL nested counts and returning values

분류에서Dev

Left join returning bad values

분류에서Dev

Returning values from an Object in Javascript

분류에서Dev

Why does the print(f.close()) return None in the following code?

분류에서Dev

Why is diff returning an incorrect value?

분류에서Dev

Why this script is returning ' 'null' is not an object'?

분류에서Dev

Why is Scrapy returning duplicate results?

분류에서Dev

Return from different function

분류에서Dev

Why am I not getting error when I store values of different types in an array in C language?

분류에서Dev

Return statement in JavaScript is not returning the right value

분류에서Dev

return statement not returning control to caller in java

분류에서Dev

Return values for appropriate arguments

분류에서Dev

How to return multiple values

Related 관련 기사

  1. 1

    print_r return 1 althought no record was saved

  2. 2

    Why is AES function returning different value?

  3. 3

    Handle different kinds of return values in a single decorator

  4. 4

    variable is returning two values, how to store them in different variable?

  5. 5

    echo and (var_dump or print_r) showing completely different things on laravel object

  6. 6

    Why is `_.map` returning undefined when I return `false`?

  7. 7

    mean of non zero elements - why are these two attempt returning different results?

  8. 8

    print_r return 1 비록 기록이 저장되지 않았지만

  9. 9

    Why does the MBR return a different offset than Ubuntu's commands?

  10. 10

    Why does this code return undefined 4 times instead of the array values?

  11. 11

    Why MySQL BIT_OR() is returning different value than PHP bitwise operation

  12. 12

    Why MySQL BIT_OR() is returning different value than PHP bitwise operation

  13. 13

    C printf() not returning values

  14. 14

    print_r과 return이 다른 값을 반환하는 이유는 무엇입니까?

  15. 15

    Why is this not returning an @@ERROR?

  16. 16

    trig functions returning incorrect values

  17. 17

    MySQL nested counts and returning values

  18. 18

    Left join returning bad values

  19. 19

    Returning values from an Object in Javascript

  20. 20

    Why does the print(f.close()) return None in the following code?

  21. 21

    Why is diff returning an incorrect value?

  22. 22

    Why this script is returning ' 'null' is not an object'?

  23. 23

    Why is Scrapy returning duplicate results?

  24. 24

    Return from different function

  25. 25

    Why am I not getting error when I store values of different types in an array in C language?

  26. 26

    Return statement in JavaScript is not returning the right value

  27. 27

    return statement not returning control to caller in java

  28. 28

    Return values for appropriate arguments

  29. 29

    How to return multiple values

뜨겁다태그

보관