Jump to content

[SOLVED] Function Problem


tsilenzio

Recommended Posts

Whenever I try to use this function on WampServer2 (Php5) and use firefox for the browser it says page connot be found, instead of an error =/

 

//<?php // -- Enable Highlighting -- //

function array_rsearch($needle, $haystack) {
    foreach ($haystack as $key => $data) {
        if(is_string($data) || is_numeric($data)) {
            if($needle == $data) {
                return true;
            }
        } else {
            if(array_rsearch($needle, $haystack) == true) {
                return true;
            }
        }
    }
}

 

NOTE: If I remove a semicolon then the page will generate with an error saying theres a missing semi colon =/

Link to comment
https://forums.phpfreaks.com/topic/86664-solved-function-problem/
Share on other sites

you do not get a 404 in firefox btu instead an infinite loop error (php sends header errors if it sense this)

 

your problem is that your funtion has no conclusion to reach if it never finds the array math (I think)

 

what exatly you trying to do

 

(note ie usually doesn't send anything but 404 and 500 errors for headers so it treats this infinte loop error (I think 410) as a 404 or what ever php sends it.

It doesn't look like it should loop infinitely.  foreach will stop after it goes through all of $haystack. 

 

You could always us in_array() to find if a value is in an array (but I'm sure you want your own function, just a suggestion)

http://us2.php.net/in_array

 

And what happens if you use ie for the browser?

Here i updated the code anyway just to prevent infintie loops or errors (although before it would have given an error not an infinite loop)

 

//<?php // -- Enable Text Highlighting -- //

function array_rsearch($needle, $haystack) {
    foreach ($haystack as $key => $data) {
        if(is_string($data) || is_numeric($data)) {
            if($needle == $data) {
                return true;
            }
        } elseif(is_array($data)) {
            if(array_rsearch($needle, $haystack) == true) {
                return true;
            }
        }
    }
}

 

And it also gives me a 404, page not found.. when it does this (same with firefox) it gives me the error almost immediately, (for other people out there who things its an infinite loop) the infinite loop will take about 4 seconds before it cuts it off..

incase for some odd reason it resides in the rest of my code ill show u my whole page almost.. this page makes links for a siebar for my site..

 

<?php
function array_rsearch($needle, $haystack) {
    foreach ($haystack as $key => $data) {
        if(is_string($data) || is_numeric($data)) {
            if($needle == $data) {
                return true;
            }
        } elseif(is_array($data)) {
            if(array_rsearch($needle, $haystack) == true) {
                return true;
            }
        }
    }
}

// Need to finish this, first need to fix the 404 error..
function compare_lkeys(&$source, $array) {
    foreach ($array as $data => $key) {
        if($array)
        if($data == $source) {
            return;
        } elseif(strtolower($data) == strtolower($source)) {
            $source = $data;
            return;
        }
    }
}

function link_add($link, $title, $group) {
    global $links;
    
    if(!isset($links)) {
        $links = array($group => array($title => $link));
    } elseif(array_rsearch($link, $links)) {
        return;
    } elseif((!array_key_exists($group, $links)) || (!array_key_exists($title, $links))) {
        //compare_lkeys($group, $title);
        $links = array_merge_recursive($links, array($group => array($title => $link)));
    } else {      
        $links = array_merge_recursive($links, array($group => array($title => $link)));
    }
}


link_add("./home.php", "Home", "Areas");
link_add("./home.php", "City", "Areas");
link_add("./home.php", "Forum", "Areas");


link_add("./home.php", "Preferences", "Places");
link_add("./home.php", "Manual", "Places");
link_add("./home.php", "Time", "Places");


link_add("./home.php", "Logout", "Things");
link_add("./home.php", "Login", "Things");
link_add("./home.php", "Day", "Things");
?>

 

as of right now its still in prototype mode

From the looks of it, if $data is an array, the function will call itself over and over.  You should try changing it to

if(array_rsearch($needle, $data) == true) {

 

That way instead of sending the same info over and over, you send the $data array.  Hope I'm seeing this right.  Try it out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.