2

I'm just looking for a MORE eloquent way of defining an array of empty error strings for a mail form then this...

 $err = array('NAME'=>'', 'EMAIL'=>'', 'ADDR'=>'', 'CS_LOC'=>'', 'ZIP'=>'','PHONE'=>'', 'COMMENTS'=>'', 'FILE'=> '');

Since I have to flush these occasionally anyway, certainly anytime the form posts, I've already set up a function like the below which I call as needed, like this.

    function clearFormErrors(&$ary) { // fill error array with empty strings
   $keys = array_keys($ary);
   $filled = array_fill_keys($keys, '');
   }        

Not a big deal [EDIT]:, but since I'm going to call this anyway, it would be nice if there were some nice 'wishlist' way I could just define the array **KEYS when I declare it**, like this...

$err = array('NAME', 'EMAIL, 'ADDR', 'CS_LOC, 'ZIP,'PHONE', 'COMMENTS, 'FILE');

Of course the above does NOT have that desired effect. I'm just curious if there is some simple directive or method that I've missed in the PHP docs that would just let me define the keys (with or without some default type). I'm sure there is a more complicated function that could be created to build a new array using the keys (now values) from my above 'wishlist' array, but it sounds like it wouldn't make the code any shorter or more 'eloquent', right?

2
  • Probably you should use your own array implementation (ArrayAccess interface: php.net/manual/en/class.arrayaccess.php) Commented Aug 3, 2015 at 18:02
  • Thanks... I'll study it to see what I can learn there. Commented Aug 3, 2015 at 18:20

1 Answer 1

6

If you have array of keys, array_fill_keys is the most elegant way to "reset" the array. The other way that makes sense is array_map, but for your simple task it seems like a little overhead.

Sign up to request clarification or add additional context in comments.

8 Comments

I thought I could only use array_fill_keys if the "keys" were numeric? In this case my keys are descriptive text strings. Now if PHP gave me something like C/C++ or Java enums, then the keys could indeed be numeric, and the lookup references would not have to be strings, and it would probably run faster. But that's a LOT of "ifs".
You can only use array_fill if the keys are numeric. array_fill_keys works for any type of keys. In this case (and many other cases), I'd sacrifice milliseconds for elegant code. Performance of such tasks doesn't matter that much.
Yes, thats what I thought, and so that's what I'm using in my clearFormErrors() for repetitive clearing. But again what I was hoping for was a more eloquent way of creating the array to begin with. Meaning, initially create the array with its named keys, without bothering to include all those empty string constructs.
I think it's eloquent to use array_fill_keys, because empty string is not like default way to start an array or anything. Why not false/null/0?
I think I understand your question. What I'm telling you is that array_fill_keys is elegant enough to use it the way you do. Array with empty strings is nothing common enough to make separate language construction to create it. What is that you don't like about $err = array_fill_keys(['NAME', 'EMAIL', 'ADDR'], ''); ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.