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
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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.