How to create an array like this

Source: Internet
Author: User
How such arrays are created



How to write the corresponding small classification equal to the large classification ID 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//Large category ID

$Dtext//Big category name

$Xhref//Small category ID

$XID _d//small classification corresponding to the large category ID

$Xtext//Small category name


Output




Large Category ID
Array (+) {[0]=> string (9) "Sidengny." [1]=> string (9) "907362758" [2]=> string (9) "907362759" [3]=> string (9) "907362764" [4]=> string (9) "90736277 5 "[5]=> string (9)" 907362795 "[6]=> string (9)" 907362787 "[7]=> string (9)" 907362784 "[8]=> string (9)" 907363 605 "[9]=> string (9)" 907363606 "[10]=> string (9)" 907363607 "[11]=> string (9)" 907363608 "[12]=> string (9)" 9 07363609 "[13]=> string (9)" 100563948 "}


Big category Name
Array {[0]=> string (8) "All Baby" [1]=> string (8) "New zone" [2]=> string (8) "Warm Coat" [3]=> string (8) "Warm Suit" [4]=&G T String (6) "Warm Pants" [5]=> string (8) "Panty series" [6]=> string (8) "Sock zone" [7]=> string (8) "Vest-T-shirt" [8]=> string (8) "Sports Bra" [ 9]=> string (8) "Clearance Area" [10]=> string (8) "Men's Zone" [11]=> string (8) "Ladies Zone" [12]=> string (8) "Gift zone" [13]=> string (8) "Recommended Classification"}



Small Category ID
Array (PNS) {[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) "90736276 3 "[8]=> string (9)" 907362765 "[9]=> string (9)" 907362766 "[10]=> string (9)" 907362767 "[11]=> string (9)" 9073 62768 "[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]=> St Ring (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)" 907 362793 "[31]=&Gt String (9) "907362794" [32]=> string (9) "907362785" [33]=> string (9) "100563948" [34]=> string (9) "100563948" [35 ]=> string (9) "100563948" [36]=> string (9) "100563948"}

Corresponding to the superior large category ID
Array (PNS) {[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]=> St Ring (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)" 907 362795 "[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]=> s Tring (9) "907362787" [31]=&Gt String (9) "907362787" [32]=> string (9) "907362784" [33]=> string (9) "100563948" [34]=> string (9) "100563948" [35 ]=> string (9) "100563948" [36]=> string (9) "100563948"}

Small category Name
Array (PNS) {[0]=> string (6) "by volume" [1]=> string (6) "By Sales" [2]=> string (6) "by new" [3]=> string (6) "by Price" [4]=> s Tring (16) "Fake Two pieces (shirt collar)" [5]=> string (7) "V-neckline" [6]=> string (8) "Business Casual" [7]=> string (8) "Fashion Home" [8]=> string (8) "Mens set "[9]=> string (8)" Women's Suit "[10]=> string (12)" Base Bra "[11]=> string (8)" Fleece Thicken "[12]=> string (8)" Cotton underwear "[13]=> String (8) "Wool underwear" [14]=> string (8) "Camel's underwear" [15]=> string (8) "Lycra underwear" [16]=> string (6) "SpongeBob" [17]=> string (10) "Thin Leggings "[18]=> string (8)" Padded Thicken "[19]=> string (8)" Men's Underwear "[20]=> string (8)" Ladies ' underwear "[21]=> string (6)" Boxer shorts "[22]=> String (8) "Plant fiber" [23]=> string (6) "Week Pants" [24]=> string (6) "Gift box" [25]=> string (4) "Cotton Sock" [26]=> string (4) "Stockings" [27 ]=> string (6) "Cotton Socks" [28]=> string (8) "Combed Cotton Socks" [29]=> string (8) "Spring and Autumn Cotton socks" [30]=> string (8) "Summer Breathable" [31]=> string (8 "Winter Thicken" [32]=> string (12) "Mens Vest-T-shirt" [33]=> string (8) "Hot Deal" [34]=> string (8) "Set recommended" [35]=> string (10) "Shirt collar recommended" [36]=> string (4) "Underwear" }




The ultimate goal is to generate JSON

"Data": {
"Cats": [
{
"id": "907362758",
"Name": "New Product Area",
"subcats": []
},
{
"id": "907362759",
"Name": "Warm Jacket",
"subcats": [
{
"id": "907362760",
"Name": "Fake two pieces (shirt collar)"
},
{
"id": "907362761",
"Name": "V-neckline"
},
{
"id": "907362762",
"Name": "Business Casual"
},
{
"id": "907362763",
"Name": "Stylish Home"
}
]
},
{


------to solve the idea----------------------
First Class classification
$a = Array (
0 =
Array (
' id ' = ' 907362758 ',
' Name ' = ' new product area ',
),
1 =
Array (
' id ' = ' 907362759 ',
' Name ' = ' warm coat ',
),
2 =
Array (
' id ' = ' 907362764 ',
' Name ' = ' warm suit ',
),
3 =
Array (
' id ' = ' 907362775 ',
' Name ' = ' warm pants ',
),
4 =
Array (
' id ' = ' 907362795 ',
' Name ' = ' Underwear series ',
),
5 =
Array (
' id ' = ' 907362787 ',
' Name ' = ' hosiery zone ',
),
6 =
Array (
' id ' = ' 907362784 ',
' Name ' = ' Vest-T-shirt ',
),
7 =
Array (
' id ' = ' 907363605 ',
' Name ' = ' sports bra ',
),
8 =
Array (
' id ' = ' 907363606 ',
' Name ' = ' clearance area ',
),
9 =
Array (
' id ' = ' 907363607 ',
' Name ' and ' Men's zone ',
),
Ten =
Array (
' id ' = ' 907363608 ',
' Name ' = ' Ladies ' zone ',
),
One by one =
Array (
' id ' = ' 907363609 ',
' Name ' = ' gift zone ',
),
The
Array (
' id ' = ' 100563948 ',
' Name ' = ' Recommended classification ',
),
);

Level Two classification
$b = Array (
0 =
Array (
' id ' = ' 907362760 ',
' Did ' = ' 907362759 ',
' name ' + ' fake two pieces (shirt collar) ',
),
1 =
Array (
' id ' = ' 907362761 ',
' Did ' = ' 907362759 ',
' Name ' and ' V-neckline ',
),
2 =
Array (
' id ' = ' 907362762 ',
' Did ' = ' 907362759 ',
' Name ' = ' Business casual ',
),
3 =
Array (
' id ' = ' 907362763 ',
' Did ' = ' 907362759 ',
' Name ' = ' Fashion Home ',
),
4 =
Array (
' id ' = ' 907362765 ',
' Did ' = ' 907362764 ',
' Name ' and ' Men's suits ',
),
5 =
Array (
' id ' = ' 907362766 ',
' Did ' = ' 907362764 ',
' Name ' and ' Women's suits ',
),
6 =
Array (
' id ' = ' 907362767 ',
' Did ' = ' 907362764 ',
' Name ' = ' base bottom underwear ',
),
7 =
Array (
' id ' = ' 907362768 ',
' Did ' = ' 907362764 ',
' Name ' = ' Add fleece thickened ',
),
8 =
Array (
' id ' = ' 907362769 ',
' Did ' = ' 907362764 ',
' Name ' = ' all cotton underwear ',
),
9 =
Array (
' id ' = ' 907362770 ',
' Did ' = ' 907362764 ',
' Name ' = ' wool underwear ',
),
Ten =
Array (
' id ' = ' 907362771 ',
' Did ' = ' 907362764 ',
' Name ' and ' Camel hair underwear ',
),
One by one =
Array (
' id ' = ' 907362772 ',
' Did ' = ' 907362764 ',
' Name ' = ' lycra underwear ',
),
The
Array (
' id ' = ' 907419493 ',
' Did ' = ' 907362764 ',
' Name ' = ' SpongeBob ',
),
+ =
Array (
' id ' = ' 907362776 ',
' Did ' = ' 907362775 ',
' Name ' = ' thin leggings ',
),
+ =
Array (
' id ' = ' 907362778 ',
' Did ' = ' 907362775 ',
' Name ' = ' Add fleece thickened ',
),
(+ =)
Array (
' id ' = ' 907362796 ',
' Did ' = ' 907362795 ',
' Name ' = ' Men's underwear ',
),
+ =
Array (
' id ' = ' 907362797 ',
' Did ' = ' 907362795 ',
' Name ' = ' Ladies underwear ',
),
+ =
Array (
' id ' = ' 907362799 ',
' Did ' = ' 907362795 ',
' Name ' = ' boxer shorts ',
),
+ =
Array (
' id ' = ' 907363601 ',
' Did ' = ' 907362795 ',
' Name ' = ' Plant fiber ',
),
+ =
Array (
' id ' = ' 907363603 ',
' Did ' = ' 907362795 ',
' Name ' = ' Week Pants ',
),
+ =
Array (
' id ' = ' 907363604 ',
' Did ' = ' 907362795 ',
' Name ' = ' gift box ',
),
+ =
Array (
' id ' = ' 907362788 ',
' Did ' = ' 907362787 ',
' Name ' = ' cotton socks ',
),
$ =
Array (
' id ' = ' 907362789 ',
' Did ' = ' 907362787 ',
' Name ' = ' stockings ',
),
$ =
Array (
' id ' = ' 907362790 ',
' Did ' = ' 907362787 ',
' Name ' = ' all cotton socks ',
),
+ =
Array (
' id ' = ' 907362791 ',
' Did ' = ' 907362787 ',
' Name ' = ' combed cotton socks ',
),
+ =
Array (
' id ' = ' 907362792 ',
' Did ' = ' 907362787 ',
' Name ' = ' Spring cotton socks ',
),
+ =
Array (
' id ' = ' 907362793 ',
' Did ' = ' 907362787 ',
' Name ' = ' Summer breathable ',
),
+ =
Array (
' id ' = ' 907362794 ',
' Did ' = ' 907362787 ',
' Name ' = ' Winter thickening ',
),
$ =
Array (
' id ' = ' 907362785 ',
' Did ' = ' 907362784 ',
' Name ' = ' Men's Vest-T-shirt ',
),
$ =
Array (
' id ' = ' 100563948 ',
' Did ' = ' 100563948 ',
' Name ' = ' Hot recommendation ',
),
(+ =)
Array (
' id ' = ' 100563948 ',
' Did ' = ' 100563948 ',
' Name ' = ' Set recommendation ',
),
+ =
Array (
' id ' = ' 100563948 ',
' Did ' = ' 100563948 ',
' Name ' = ' shirt collar recommendation ',
),
+ =
Array (
' id ' = ' 100563948 ',
' Did ' = ' 100563948 ',
' Name ' = ' underwear ',
),
);

Transcribe and format first-class classification to $res
foreach ($a as $v) {
$v [' subcats '] = array ();
$res [$v [' id ']] = $v;
}

Traverse level Two classification, fill in the corresponding first class classification
foreach ($b as $v) {
$res [$v [' Did ']][' subcats ' [] = $v;
}

Print_r ($res); Print it out and see what else you need.
Array
(
[907362758] = = Array
(
[id] = 907362758
[Name] + new Area
[subcats] = = Array
(
)

)

[907362759] = = Array
(
[id] = 907362759
[Name] = Warmer
[subcats] = = Array
(
[0] = = Array
(
[id] = 907362760
[Did] = 907362759
[Name] + two pieces (shirt collar)
)

[1] = = Array
(
[id] = 907362761
[Did] = 907362759
[Name] = v-neck top
)

[2] = = Array
(
[id] = 907362762
[Did] = 907362759
[Name] + = Business Casual
)

[3] = = Array
(
[id] = 907362763
[Did] = 907362759
[Name] = Fashion Home
)

)

)

[907362764] = = Array
(
[id] = 907362764
[Name] + thermal Set
[subcats] = = Array
(
[0] = = Array
(
[id] = 907362765
[Did] = 907362764
[Name] + men's suit
)

[1] = = Array
(
[id] = 907362766
[Did] = 907362764
[Name] and lady's suit
)

[2] = = Array
(
[id] = 907362767
[Did] = 907362764
[name] + Base Bottom Bra
)

[3] = = Array
(
[id] = 907362768
[Did] = 907362764
[Name] + Velvet thickened
)

[4] = = Array
(
[id] = 907362769
[Did] = 907362764
[Name] and all cotton underwear
)

[5] = = Array
(
[id] = 907362770
[Did] = 907362764
[name] + wool underwear
)

[6] = = Array
(
[id] = 907362771
[Did] = 907362764
[Name] and camel's underwear
)

[7] = = Array
(
[id] = 907362772
[Did] = 907362764
[name] + lycra underwear
)

[8] = = Array
(
[id] = 907419493
[Did] = 907362764
[Name] = SpongeBob
)

)

)

[907362775] = = Array
(
[id] = 907362775
[Name] and thermal pants
[subcats] = = Array
(
[0] = = Array
(
[id] = 907362776
[Did] = 907362775
[name] and slim leggings
)

[1] = = Array
(
[id] = 907362778
[Did] = 907362775
[Name] + Velvet thickened
)

)

)
......
Too much to post, meaning to
  • 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.