Jump to content

return an associative array from a function


rbrown

Recommended Posts

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

 

 

 

 

 

 

 

Link to comment
Share on other sites

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!";
}

?>

Link to comment
Share on other sites

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";
    }
  }
?>

Link to comment
Share on other sites

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

 

 

 

 

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.