One-dimensional array objects into two-dimensional arrays

Source: Internet
Author: User

A question about PHP turning a one-dimensional array loaded with item into a two-dimensional array

background : When you add a post, when you select a tag, you can select more than one, and the backend interface is a two-dimensional array that needs to be passed in.

/** * @Title   Modify selected Content * @Params  $id         Record ID * @Params  $tags       Tag Group Array (*                                      ' name '   = > ' Desktop Culture ', *                                      ' tag_id ' = ' 2 ' *                                  ) */public function Update ($id = 0, $tags = Array ())

puzzle: back-end interface needs to pass in a two-dimensional array, I in the foreground Ajax pass a one-dimensional array of objects stored in the past, according to PHP syntax, the PHP controller automatically into a two-dimensional array.

JS does not have a two-dimensional array, you can use a one-dimensional array, which is placed in the object. This way to achieve the goal.

I pass the past content from the foreground:

Figure One:

What I received in the PHP controller:

Figure II:

It's obviously,php when it receives this one-dimensional array, it turns the objects in one dimension into arrays. nice~ so I can go directly to the interface.

The code is this:

Map reads the current object, gets its properties into an object, and then puts the object into an array. Yes, I can.

$tags. Find (' span '). Filter ('. Active '). Map (function(i) {84Wvar$ This= $( This); 947varTagID = $ This. attr (' Data-tagid '), 948 name = $ This. attr (' Data-name '); 949                var tagitem = {}; 950 tagitem[' name '] = name; 951 tagitem[' tag_id '] = tagid; 952 Tagsarr[i] = tagitem; 953             });957 958vardata = { 959Handler:type,969Tags:tagsarr970};

PHP directly receives this array just fine. Var_dump came out to see the data structure of our figure II.

The method of jquery, filter () and map (), one to filter, one to match each element in the current set, and to produce a new jquery object.

In the document: If you want to work with a simple array or object, use Jquery.map () instead.

Since the return value is an array of jquery packages, it is usually used to turn it into a normal array using the Get () method.

It's easy to find all the tabs you've checked, and you can put them in an array when you select them. For example, to get multiple selected labels now

Remember, the next time you use, you can directly take the code with 0.0

One-dimensional array objects into two-dimensional arrays

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.