Excuse me under such example how to correctly index table singular group

Source: Internet
Author: User
It feels a little bit around, so I'm going to tell you the problem.
The client form has multiple arrays, such as the item array, an info array, for example:







Now to validate the form, all 4 items must be filled

I encapsulated a function in the background, directly verify that $_request[' ID ' is empty.
But it is troublesome to encounter an array, because of the reason for the function, when the value is passed.

Function Validator ($name) {
Determine if $_request[' $name '] is empty
}

Validator (' id '), so it's better to operate
If not the singular group of the table is OK, the table singular group is very troublesome, need to put the element correct index can

Now I want to ask how the operation can achieve this effect
Validator (' item[' title "] ')

function can be indexed as normal within the
$_request[' item ' [' title ']

Regardless of the array layer, just pass in the correct array element string, you can correctly index the element's table items, cannot find such a function, can only use the method of disassembling the string, do not know there is no better way

It's a good way to get to the unknown item and array index title, of course, this is a 2-layer paradigm, if you can achieve more layers and better


Reply to discussion (solution)

There seems to be no way to implement this by using the array function

Array_walk_recursive should be all right.
The key to the operation after the empty sentence

Array_walk_recursive passed in an array, the key is to pass in the table singular group of elements, the string is mostly

$ar = Array ('  item ' = = Array (' title ' + = ', ' intro ' = ') ',  ' info ' = = Array (' userid ' = ', ' Userna Me ' + '), function validator ($v, $k) {  if (empty ($v)) echo "$k cannot be empty \ n";} Array_walk_recursive ($ar, ' validator ');
Title cannot be empty intro cannot be empty UserID cannot be empty username cannot be empty

Of course, most of the printed content does not meet your requirements.
So I said, "The key is the operation after the empty sentence."

I see, thanks, it's all read and judged.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.