rbrown Posted January 21, 2008 Share Posted January 21, 2008 I have a function that processes data and stores error codes in an array within the function Like: function Do_Something($what_to_do){ Some checking script... error_array['error_1']='error one happened'; More checking script... error_array['error_2']='error two happened'; etc... } I've tried to return the array so I can either just echo error_array['error_2']; or loop through the array or even do a print_r I have tried renaming it, before the function ends and tried using extract afterwards. And I can't seem to get it. If I echo it or loop through within the function it works but I would like to be able to call the errors after it has processed farther down in another script section without rerunning the function. Thanks, Bob Quote Link to comment https://forums.phpfreaks.com/topic/87103-return-an-associative-array-from-a-function/ Share on other sites More sharing options...
marcus Posted January 21, 2008 Share Posted January 21, 2008 hmm <?php function errors($value){ $errors = array(); if(strlen($value) < 3){ $errors[] = "The value must be greater than 3 characters"; }else { if(strlen($value) > 32){ $errors[] = "The value must be less than 32 characters"; } } return $errors; } $error_check = errors('some values'); if(count($error_check) > 0){ foreach($error_check AS $error){ echo $error . "<br>\n"; } }else { echo "No errors, proceed with caution!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87103-return-an-associative-array-from-a-function/#findComment-445470 Share on other sites More sharing options...
rhodesa Posted January 21, 2008 Share Posted January 21, 2008 The best way (in my opinion) to approach this is with a variable passed by reference: <?php function Do_Something($what_to_do, &$errors = array()){ //Some checking script... $errors[]='error one happened'; //More checking script... $errors[]='error two happened'; //etc... return !count($errors); //Return true if no errors, false if there are } $errors = array(); if(!Do_Something('foo',$errors)){ echo "ERRORS:\n"; foreach($errors as $error){ echo " - {$error}\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87103-return-an-associative-array-from-a-function/#findComment-445484 Share on other sites More sharing options...
rbrown Posted January 21, 2008 Author Share Posted January 21, 2008 Problem is I need the keys returned... So I don't have to figure out what error is what based on the order... The errors can be returned in any order, for example, it could fail at anyone of these points and it will still finish processing: error_array['data_prep_response'] error_array['data_parse_response'] error_array['data_ready_response'] error_array['data_sent_response'] So it could send back this order: error_array['data_prep_response'] error_array['data_ready_response'] error_array['data_sent_response'] or error_array['data_ready_response'] error_array['data_sent_response'] So if I need to call if(error_array['data_prep_response']==533) I know that I'm getting the error_array['data_prep_response'] error line. Instead of trying to do a ton of math calculations with all the combinations to return what error I want. I know I could set a var reguardless if it errored and use 0-whatever to return it... But I don't want to have to remember that error_array[1] equals error_array['data_prep_response'] I'd rather just call the error by name that I'm looking for. That way when I rewrite, fix, or tweak it a month or a year from now I'm not scratching my head wondering what is was or having to relearn the whole script. Follow? Thanks, Bob Quote Link to comment https://forums.phpfreaks.com/topic/87103-return-an-associative-array-from-a-function/#findComment-445548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.