How to create such an array

Source: Internet
Author: User
How to create such an array and write the corresponding small category equal to the big category ID to subCats
Array (
Id => "$ Xhref .....",
Name => "$ Xtext ....",
)

$ Dafenleishu = count ($ Dhref );
$ Xiaofenleishu = count ($ Xhref );

For ($ x = 1; $ x <= $ dafenleishu-1; $ x ++ ){
$ Cats [] = array (
Id => "$ Dhref [$ x]",
Name => "$ Dtext [$ x]",
SubCats => array ()
);

}


$ Dhref // Big Classification ID

$ Dtext // Big classification name

$ Xhref // small category ID

$ XID_D // The ID of a small-Category pair.

$ Xtext // small category name


Output



Big classification IDarray (14) {[0] => string (9) "sidengny. "[1] => string (9)" 907362758 "[2] => string (9)" 907362759 "[3] => string (9) "907362764" [4] => string (9) "907362775" [5] => string (9) "907362795" [6] => string (9) "907362787" [7] => string (9) "907362784" [8] => string (9) "907363605" [9] => string (9) "907363606" [10] => string (9) "907363607" [11] => string (9) "907363608" [12] => string (9) "907363609" [13] => string (9) "100563948"} big classification name array (14) {[0] => string (8) "All Baby" [1] => string (8) "New area" [2] => string (8) "warm top" [3] => string (8) "Warm Suit" [4] => string (6) "warm pants" [5] => string (8) "panties series" [6] => string (8) "so "[7] => string (8) "vest-T-shirt" [8] => string (8) "Sports Bra" [9] => string (8) "clearance area" [10] => string (8) "men's area" [11] => string (8) "Ladies" [12] => string (8) "" [13] => string (8) ""} IDarray (37) {[0] => string (9) "sidengny. "[1] => string (9)" sidengny. "[2] => string (9)" sidengny. "[3] => string (9)" sidengny. "[4] => string (9)" 907362760 "[5] => string (9)" 907362761 "[6] => string (9) "907362762" [7] => string (9) "907362763" [8] => string (9) "907362765" [9] => string (9) "907362766" [10] => string (9) "907362767" [11] => string (9) "907362768" [12] => string (9) "907362769" [13] => string (9) "907362770" [14] => string (9) "907362771" [15] => string (9) "907362772" [16] => string (9) "907419493" [17] => string (9) "907362776" [18] => string (9) "907362778" [19] => string (9) "907362796" [20] => string (9) "907362797" [21] => string (9) "907362799" [22] => string (9) "907363601" [23] => string (9) "907363603" [24] => string (9) "907363604" [25] => string (9) "907362788" [26] => string (9) "907362789" [27] => string (9) "907362790" [28] => string (9) "907362791" [29] => string (9) "907362792" [30] => string (9) "907362793" [31] => string (9) "907362794" [32] => string (9) "907362785" [33] => string (9) "100563948" [34] => string (9) "100563948" [35] => string (9) "100563948" [36] => string (9) "100563948"} corresponds to IDarray (37) {[0] => string (9) "engny. tma "[1] => string (9)" engny. tma "[2] => string (9)" engny. tma "[3] => string (9)" engny. tma "[4] => string (9)" 907362759 "[5] => string (9)" 907362759 "[6] => string (9) "907362759" [7] => string (9) "907362759" [8] => string (9) "907362764" [9] => string (9) "907362764" [10] => string (9) "907362764" [11] => string (9) "907362764" [12] => string (9) "907362764" [13] => string (9) "907362764" [14] => string (9) "907362764" [15] => string (9) "907362764" [16] => string (9) "907362764" [17] => string (9) "907362775" [18] => string (9) "907362775" [19] => string (9) "907362795" [20] => string (9) "907362795" [21] => string (9) "907362795" [22] => string (9) "907362795" [23] => string (9) "907362795" [24] => string (9) "907362795" [25] => string (9) "907362787" [26] => string (9) "907362787" [27] => string (9) "907362787" [28] => string (9) "907362787" [29] => string (9) "907362787" [30] => string (9) "907362787" [31] => string (9) "907362787" [32] => string (9) "907362784" [33] => string (9) "100563948" [34] => string (9) "100563948" [35] => string (9) "100563948" [36] => string (9) "100563948"} small classification name array (37) {[0] => string (6) "by synthesis" [1] => string (6) "by sales" [2] => string (6) "by new product" [3] => string (6) "by price" [4] => string (16) "Fake two-piece shirt collar" [5] => string (7) "v-neck top" [6] => string (8) "Business and Leisure" [7] => string (8) "Fashion Home" [8] => string (8) "men's suit" [9] => string (8) "Ladies set" [10] => string (12) "basic underfloor lingerie" [11] => string (8) "plus velvet thickening" [12] => string (8) "Cotton underwear" [13] => string (8) "wool underwear" [14] => string (8) "camel underwear" [15] => string (8) "Lycra underwear" [16] => string (6) "birthDate" [17] => string (10) "thin leggings" [18] => string (8) "Velvet thickening" [19] => string (8) "men's underwear" [20] => string (8) "women's underwear" [21] => string (6) "Slice pants" [22] => string (8) "plant fiber" [23] => string (6) "week pants" [24] => string (6) "Gift Box" [25] => string (4) "cotton so" [26] => string (4) "stockings" [27] => string (6) "Cotton so" [28] => string (8) "combed cotton so" [29] => string (8) "spring and autumn cotton so" [30] => string (8) "Summer breathable" [31] => string (8) "winter thickening" [32] => string (12) "Men's vest-t-shirt" [33] => string (8) "Hot sale recommendation" [34] => string (8) "Suit recommendations" [35] => string (10) "shirt collar recommendations" [36] => string (4) "panties "}


The ultimate goal is to generate json

"Data ":{
"Cats ":[
{
"Id": "907362758 ",
"Name": "new product area ",
"SubCats": []
},
{
"Id": "907362759 ",
"Name": "Warm top ",
"SubCats ":[
{
"Id": "907362760 ",
"Name": "Two fake shirts )"
},
{
"Id": "907362761 ",
"Name": "v-neck"
},
{
"Id": "907362762 ",
"Name": "Business and Leisure"
},
{
"Id": "907362763 ",
"Name": "Fashion Home"
}
]
},
{


Reply to discussion (solution)

for ($x=1;$x<=$dafenleishu-1;$x++) {for ($y=4;$y<=$xiaofenleishu-4;$y++) {if ($XID_D[$x]==$Dhref[$y]) {$cats[$x]=array(id=>"$Dhref[$x]",name=>"$Dtext[$x]",      subCats=>$subCats[$y]= array(id=>"$Xhref[$y]",name=>"$Xtext[$y]",) );}else{$cats[$x]=array(id=>"$Dhref[$x]",name=>"$Dtext[$x]",subCats=>$subCats[]= array());}      }}




This does not seem to work out.

Reference http://bbs.csdn.net/topics/370094009

If you want us to do this for you, post the var_export result of the original data.

Thank you for continuing to learn. I feel that is what I need. if I try my best to solve the problem by myself, I will continue to learn the main purpose.


Level 1

Array (0 => array ('id' => '123', 'name' => 'new product region ',), 1 => array ('id' => '000000', 'name' => 'warm upload',), 2 => array ('id' => '000000 ', 'name' => 'warm suit ',), 3 => array ('id' => '123', 'name' => 'warm pants ',), 4 => array ('id' => '000000', 'name' => 'underpants series',), 5 => array ('id' => '000000 ', 'name' => 'soup area ',), 6 => array ('id' => '000000', 'name' => 'vest-T-shirt ',), 7 => array ('id' => '000000', 'name' => 'Sports bra ',), 8 => array ('id' => '000000 ', 'name' => 'clearance region',), 9 => array ('id' => '123', 'name' => 'Men region ',), 10 => array ('id' => '123', 'name' => 'Ladies dashboard ',), 11 => array ('id' => '123 ', 'name' => 'gift box ',), 12 => array ('id' => '123456', 'name' => 'Recommended category ',),)




Level 2

Array (0 => array ('id' => '000000', 'did' => '000000', 'name' => 'fake two-piece (shirt collar )',), 1 => array ('id' => '000000', 'did' => '000000', 'name' => 'V collar upgrade ',), 2 => array ('id' => '000000', 'did' => '000000', 'name' => 'business casual ',), 3 => array ('id' => '000000', 'did' => '000000', 'name' => 'Fashion home ',), 4 => array ('id' => '000000', 'did' => '000000', 'name' => 'Men's package ',), 5 => array ('id' => '000000', 'did' => '000000', 'name' => 'Ladies bundle ',), 6 => array ('id' => '000000', 'did' => '000000', 'name' => 'basic underweary ',), 7 => array ('id' => '000000', 'did' => '000000', 'name' => 'Velvet thickening ',), 8 => array ('id' => '000000', 'did' => '000000', 'name' => 'cotton underwed ',), 9 => array ('id' => '000000', 'did' => '000000', 'name' => 'econnoisseurs ',), 10 => array ('id' => '000000', 'did' => '000000', 'name' => 'camel underweder ',), 11 => array ('id' => '000000', 'did' => '000000', 'name' => 'Lycra underweary ',), 12 => array ('id' => '123', 'did' => '123', 'name' => 'current year ',), 13 => array ('id' => '123', 'did' => '123', 'name' => 'thin leggings ',), 14 => array ('id' => '000000', 'did' => '000000', 'name' => 'Velvet thickening ',), 15 => array ('id' => '000000', 'did' => '000000', 'name' => 'Men's underweer ',), 16 => array ('id' => '000000', 'did' => '000000', 'name' => 'Ladies underput ',), 17 => array ('id' => '000000', 'did' => '000000', 'name' => 'slashes ',), 18 => array ('id' => '123', 'did' => '123', 'name' => 'plant fiber ',), 19 => array ('id' => '000000', 'did' => '000000', 'name' => 'weeks ',), 20 => array ('id' => '123', 'did' => '123', 'name' => 'gift box ',), 21 => array ('id' => '000000', 'did' => '000000', 'name' => 'sows ',), 22 => array ('id' => '000000', 'did' => '000000', 'name' => 'stockings ',), 23 => array ('id' => '000000', 'did' => '000000', 'name' => 'cotton so ',), 24 => array ('id' => '000000', 'did' => '000000', 'name' => 'combed cotton so ',), 25 => array ('id' => '000000', 'did' => '000000', 'name' => 'Spring and autumn cotton so ',), 26 => array ('id' => '000000', 'did' => '000000', 'name' => 'Summer breathable ',), 27 => array ('id' => '000000', 'did' => '000000', 'name' => 'Winter thickening ',), 28 => array ('id' => '000000', 'did' => '000000', 'name' => 'Men's vest-T-shirt ',), 29 => array ('id' => '000000', 'did' => '000000', 'name' => 'hot recommendation ',), 30 => array ('id' => '000000', 'did' => '000000', 'name' => 'Suit recommend ',), 31 => array ('id' => '000000', 'did' => '000000', 'name' => 'shirt collar recommend ',), 32 => array ('id' => '000000', 'did' => '000000', 'name' => 'underwed ',),)


Reference http://bbs.csdn.net/topics/370094009

If you want us to do this for you, post the var_export result of the original data.




Not Handled. please solve it.

// Level-1 Category $ a = array (0 => array ('id' => '000000', 'name' => 'new thing ',), 1 => array ('id' => '000000', 'name' => 'warm upload',), 2 => array ('id' => '000000 ', 'name' => 'warm suit ',), 3 => array ('id' => '123', 'name' => 'warm pants ',), 4 => array ('id' => '000000', 'name' => 'underpants series',), 5 => array ('id' => '000000 ', 'name' => 'soup area ',), 6 => array ('id' => '000000', 'name' => 'vest-T-shirt ',), 7 => array ('id' => '000000', 'name' => 'Sports bra ',), 8 => array ('id' => '000000 ', 'name' => 'clearance region',), 9 => array ('id' => '123', 'name' => 'Men region ',), 10 => array ('id' => '123', 'name' => 'Ladies dashboard ',), 11 => array ('id' => '123 ', 'name' => 'gift-giving region',), 12 => array ('id' => '000000', 'name' => 'Recommended category ',),); // second-level classification $ B = array (0 => array ('id' => '123', 'did' => '123 ', 'name' => 'two-piece (shirt collar) ',), 1 => array ('id' => '123', 'did' => '123 ', 'name' => 'V collar Mount',), 2 => array ('id' => '123', 'did' => '123 ', 'name' => 'business casual ',), 3 => array ('id' => '123', 'did' => '123 ', 'name' => 'Fashion home',), 4 => array ('id' => '123', 'did' => '123 ', 'name' => 'Men's bundle ',), 5 => array ('id' => '123', 'did' => '123 ', 'name' => 'Ladies bundle ',), 6 => array ('id' => '123', 'did' => '123 ', 'name' => 'underfloor underweder',), 7 => array ('id' => '123', 'did' => '123 ', 'name' => 'Velvet thickening ',), 8 => array ('id' => '123', 'did' => '123 ', 'name' => 'cotton underweder',), 9 => array ('id' => '123', 'did' => '123 ', 'name' => 'econnoisseurs ',), 10 => array ('id' => '123', 'did' => '123 ', 'name' => 'Velvet underwee',), 11 => array ('id' => '123', 'did' => '123 ', 'name' => 'Lycra underwee',), 12 => array ('id' => '123456', 'did' => '123456 ', 'name' => 'current year',), 13 => array ('id' => '123', 'did' => '123 ', 'name' => 'thin leggings ',), 14 => array ('id' => '123', 'did' => '123 ', 'name' => 'Velvet thickening ',), 15 => array ('id' => '123', 'did' => '123 ', 'name' => 'Men's underwee',), 16 => array ('id' => '123', 'did' => '123 ', 'name' => 'Ladies underput',), 17 => array ('id' => '123', 'did' => '123 ', 'name' => 'slashes ',), 18 => array ('id' => '123', 'did' => '123 ', 'name' => 'plant fiber ',), 19 => array ('id' => '123', 'did' => '123 ', 'name' => 'week pants ',), 20 => array ('id' => '123', 'did' => '123 ', 'name' => 'gift box',), 21 => array ('id' => '123', 'did' => '123 ', 'name' => 'token',), 22 => array ('id' => '123', 'did' => '123 ', 'name' => 'stockings ',), 23 => array ('id' => '123', 'did' => '123 ', 'name' => 'cotton token',), 24 => array ('id' => '123', 'did' => '123 ', 'name' => 'combed so ',), 25 => array ('id' => '123', 'did' => '123 ', 'name' => 'Spring and autumn cotton so ',), 26 => array ('id' => '123', 'did' => '123 ', 'name' => 'Summer breathable ',), 27 => array ('id' => '123', 'did' => '123 ', 'name' => 'Winter thickening ',), 28 => array ('id' => '123', 'did' => '123 ', 'name' => 'Men's vest-T-shirt ',), 29 => array ('id' => '123', 'did' => '123 ', 'name' => 'hot sale recommend',), 30 => array ('id' => '123', 'did' => '123 ', 'name' => 'Suit recommend',), 31 => array ('id' => '123', 'did' => '123 ', 'name' => 'shirt collar recommend',), 32 => array ('id' => '123', 'did' => '123 ', 'name' => 'underwee',),); // Copy and format the level-1 category to $ resforeach ($ a as $ v) {$ v ['subcats'] = array (); $ res [$ v ['id'] = $ v ;}// traverses the second-level category, enter foreach ($ B as $ v) {$ res [$ v ['did'] ['subcats'] [] = $ v;} in the corresponding level-1 category ;} print_r ($ res); // Print it out to see what else do you need
Array ([907362758] => Array ([id] => 907362758 [name] => new product area [subCats] => Array ()) [907362759] => Array ([id] => 907362759 [name] => warm top [subCats] => Array ([0] => Array ([id] => 907362760 [Did] => 907362759 [name] => Fake two (shirt collar )) [1] => Array ([id] => 907362761 [Did] => 907362759 [name] => V-neck top) [2] => Array ([id] => 907362762 [Did] => 907362759 [name] => business and leisure) [3] => Array ([id] => 907362763 [Did] => 907362759 [name] => Fashion Home ))) [907362764] => Array ([id] => 907362764 [name] => warm-up set [subCats] => Array ([0] => Array ([id] => 907362765 [Did] => 907362764 [name] => Men's suits) [1] => Array ([id] => 907362766 [Did] => 907362764 [name] => ladies set) [2] => Array ([id] => 907362767 [Did] => 907362764 [name] => basic underfloor lingerie) [3] => Array ([id] => 907362768 [Did] => 907362764 [name] => velvet thickening) [4] => Array ([id] => 907362769 [Did] => 907362764 [name] => cotton underwear) [5] => Array ([id] => 907362770 [Did] => 907362764 [name] => wool lingerie) [6] => Array ([id] => 907362771 [Did] => 907362764 [name] => camel velvet underwear) [7] => Array ([id] => 907362772 [Did] => 907362764 [name] => Lycra lingerie) [8] => Array ([id] => 907419493 [Did] => 907362764 [name] => current year ))) [907362775] => Array ([id] => 907362775 [name] => warm pants [subCats] => Array ([0] => Array ([id] => 907362776 [did] => 907362775 [name] => thin leggings) [1] => Array ([id] => 907362778 [Did] => 907362775 [name] => velvet thickening )))......
Too many posts, meaning

After an afternoon of hard work, you have two lines of code to solve the problem.

Thanks

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.