I don't know how to write a two-dimensional array.

Source: Internet
Author: User
There is a two-dimensional array. I don't know how to write this post. it was last edited by rightnow5 at 20:26:33.

Two-dimensional array PHP



This is an effect demonstration. some black categories have a small icon new or hot on the right.
You can use the following settings to control which category to display new or hot
For example, to display a new icon in the second link of the first category, set in the background.
The first link of the second group category should be displayed. new is set to in the background for control.




Foreach ($ jsonArray as $ I => I I $ jsonObject) {// output level-1 category echo 'group link'; foreach ($ array as $ k => $ id) {// outputs the second-level category. some icons need to be set for the second-level category to obtain the user-set parameters. how can I determine which sub-category should be added with the icon, then output the Image class echo 'sublinks under this group 'in that category ';}}



If you set only one sub-category, for example, and, the new icon will be determined.
I don't know how to write php.


Reply to discussion (solution)

How did you write?

 '5: 3, 1: 2', 'new1' => '4: 2', 'new2' => '2: 2 ',); // process it into the structure of array (row => array (Column => class) $ row_class = array (); foreach ($ setting_clsss as $ key => $ value) {if (strpos ($ value, ',') = false) {$ value. = ',';} foreach (explode (',', $ value) as $ class) {if (! $ Class) continue; list ($ row, $ column) = explode (':', $ class); $ row_class [$ row] [$ column] = $ key; // multiple objects can be represented in arrays.} $ links = array (1, 2), array (1, 2), array (1, 2), array (1, 2), array (1, 2, 3),); foreach ($ links as $ row => $ link) {// output level-1 classification $ row + = 1; // from 0 start so + 1 echo 'group link ', $ row,': '; foreach ($ link as $ column => $ id) {// output level 2 classification, some icons need to be set for level-2 classification. the user-defined parameters are obtained. how can I determine which sub-category to add icons? then, the image class $ column + = 1 is output for that category; // start from 0 so + 1 echo 'connection', $ column; if (isset ($ row_class [$ row] [$ column]) {echo '-', $ row_class [$ row] [$ column];} echo '';} echo" \ n ";}

This post was last edited by xuzuning at 11:07:19 on

$ Ar = array ('blod' => '5: 3, 1: 2', 'new1' => '4: 2', 'new2' => '2: 2',); foreach ($ ar as $ k => $ r) {foreach (array_chunk (preg_split ('/[,:]/', $ r), 2) as $ v) echo "$ k: $ v [0] row $ v [1] column \ n ";}
Blod: 5 rows and 3 columns
Blod: 1 row and 2 columns
New1: 4 rows and 2 columns
New2: 2 rows and 2 columns

Write preg_split ('/[,:]/', $ r)
Preg_split ('/\ D +/', $ r)
More universal

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.