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