When we are developing WordPress and acquiring data, we need to clean up the data. At this time, we may need to remove null and empty values from the array, and retain non null and non empty values. To facilitate these operations, I have set several callback functions for data judgment.
Judge that the data is not null
To judge whether the data is not empty, we can directly use isset. So many people, like me, want to filter out non empty characters in the array and also directly use isset as a callback function:
$data= arrayfilter($data,isset);
But the above function directly reports:
arrayfilter() expects parameter 2 to be a valid callback, function isset not found or invalid function name
This is because isset is a language constructor rather than a function, so it can only be used! Isnull to determine non null values, and then use closures:
$data= arrayfilter($data, function($item){return !isnull($item);});
There are many such arrays in the program that need to be filtered out of null values. It seems a bit inelegant to apply the closure function every time, so I defined a function called isexists.
I've been thinking about this function name for a long time. I wanted to write this function as a callback function for a long time, but I didn't have a good name. Then I saw fileexists, functionexists, and methodexists. I suddenly got inspiration, and I was excited to write the isexists function:
if(!functionexists(isexists)){function isexists($var){return isset($var);}}
Then it can be directly used for callback functions:
$data= arrayfilter($data,isexists);
Judge whether the string is empty
In PHP, empty values can be determined by using empty, but one special note is that this function will also consider the string 0 as empty. In many cases, we do not want it. For example, when the user enters a form, 0 is entered, but it has already been entered, and it is not empty. So I also created a function called ISBLANK.
Ha ha, isn't it a good name? Blank means blank. If there is 0, it is not blank. A good name is really 80% of the success in many cases. Sometimes when writing programs, I never thought that a good name would really taste bad. I tried to write isnotempty before, but I didn't think it was elegant. I thought my code would also become garbage by the way, because this is not thinking from a positive perspective. ISBLANK is different, and it is now much taller:
if(!functionexists(isblank)){function isblank($var){return empty($var)&&!isnumeric($var);}}
Similarly, if the string is not empty, I have also defined a function called ispopulated, which literally means that if it has been filled, it is not empty:
if(!functionexists(ispopulated)){function ispopulated($var){return !isblank($var);}}
In this way, sometimes when we clean up form data, for example, we need to clean up empty fields, but we can also use the following directly if we keep the 0 input:
$data= arrayfilter($data,ispopulated);
Ha ha, these three functions are very simple, but I still think I'm great, because I think these three functions are well named and can be used at a glance. In addition, the calling method of closure functions has been removed in many places, and the program has become much more elegant.
That's why we say programmers are the simplest group of people, because they will be happy for a long time if they have a good name.