Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请问下这样的例子如何正确索引表单数组

请问下这样的例子如何正确索引表单数组

Jun 23, 2016 pm 01:57 PM
example correct index form

感觉说起来有点绕,这样吧,我把遇到的这个问题说明下
客户端表单有多个数组,比如item数组,info数组,例如:







现在要验证表单,这4个项都要必填

我后台封装了一个函数,直接验证$_REQUEST['id'] 为空即可
但是遇到数组就很麻烦,因为函数的原因,传值的时候是这样传的

function validator($name){
    判断$_REQUEST['$name']是否为空
}

validator(‘id’),这样就比较好操作
如果不是表单数组还好,表单数组就非常麻烦,需要,把元素正确的索引才可以

现在我想问的是如何操作可以实现这样的效果
validator('item["title"]')

函数内可以正常的索引为
$_REQUEST['item']['title']

无论数组层数,只要传进去正确的数组元素字符串,就可以正确索引该元素的表单项,找不到这样的函数,只能用拆解字符串的办法,不知道有没有更好的办法

说白了就是如何正确的获取到未知的item和数组索引title,当然这是个2层的范例,如果能实现更多层更好


回复讨论(解决方案)

貌似没有办法通过数组的函数实现这个

array_walk_recursive 应该就可以了
关键在与判空后的操作

array_walk_recursive 传入的是数组,关键是传入的是表单数组中的元素,字符串居多

$ar = array(  'item' => array('title' => '', 'intro' => ''),  'info' => array('userid' => '', 'username' => ''),);function validator($v, $k) {  if(empty($v)) echo "$k 不能为空\n";}array_walk_recursive($ar, 'validator');
Copy after login
title 不能为空intro 不能为空userid 不能为空username 不能为空
Copy after login

当然打印的内容多半不符合你的要求
所以我说“关键在与判空后的操作”

明白了,谢谢,是全部读取判断

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are constants in C language? Can you give an example? What are constants in C language? Can you give an example? Aug 28, 2023 pm 10:45 PM

A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

How to use JavaScript to realize the automatic prompt function of the input box content of the form? How to use JavaScript to realize the automatic prompt function of the input box content of the form? Oct 20, 2023 pm 04:01 PM

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

How to use JavaScript to implement real-time verification of the input box content of a form? How to use JavaScript to implement real-time verification of the input box content of a form? Oct 18, 2023 am 08:47 AM

How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML

What are the Oracle index types? What are the Oracle index types? Nov 16, 2023 am 09:59 AM

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

How to use HTML, CSS and jQuery to realize the advanced function of automatic saving of forms How to use HTML, CSS and jQuery to realize the advanced function of automatic saving of forms Oct 28, 2023 am 08:20 AM

How to use HTML, CSS and jQuery to implement the advanced function of automatic saving of forms. Forms are one of the most common elements in modern web applications. When users enter form data, how to implement the automatic saving function can not only improve the user experience, but also ensure data security. This article will introduce how to use HTML, CSS and jQuery to implement the automatic saving function of the form, and attach specific code examples. 1. Structure of HTML form. Let’s first create a simple HTML form.

Tips for using Laravel form classes: ways to improve efficiency Tips for using Laravel form classes: ways to improve efficiency Mar 11, 2024 pm 12:51 PM

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel

How to create a form with a floating prompt using HTML, CSS and jQuery How to create a form with a floating prompt using HTML, CSS and jQuery Oct 25, 2023 am 10:48 AM

How to create a form with floating prompts using HTML, CSS and jQuery In modern web design, forms are one of the indispensable components. In order to improve user experience, we often need to add some floating prompts to the form to guide users to fill in the form correctly. This article will introduce how to use HTML, CSS and jQuery to create a form with floating prompts, and provide specific code examples. First, we need to create the HTML form. In the form we need to add some input fields, and

How to set up a secure HTTPS connection for a PHP form? How to set up a secure HTTPS connection for a PHP form? Aug 17, 2023 pm 03:25 PM

How to set up a secure HTTPS connection for a PHP form? As the Internet develops, security becomes more and more important in web development. The encrypted transmission protocol HTTPS plays a key role in protecting data transmission. When using PHP forms for data transmission, we can take some measures to ensure the security of the connection. This article will guide you on how to set up a secure HTTPS connection for PHP forms, with some code examples. Purchase an SSL Certificate First, you need to purchase an SSL Certificate. SSL certificate is a guaranteed website

See all articles