カテゴリとサブカテゴリのリストが表示されるこのコードはありますが、私はchildCategoriesの内部にある必要があります.2段階以上であれば既に追加できます。それを追加する。シナリオは次のようになります: -多次元配列の子に追加
親カテゴリは「電子」で、サブカテゴリはスマートフォン内に「スマートフォン」と「ラップトップ」、AsusとSamsungがあり、Sameはラップトップになります。ラップトップのカテゴリには、「Alienware Series」と「Vostro Series」があります。
電子
- >スマートフォン
- > Asusの
- >サムスン
- >
をラップトップ - > HP
- > Dell ---> Alienwareシリーズ、Vostroシリーズ
私は配列はこのようになりたい...ここ
Array
(
[categoryId] => 3
[title] => Electronic
[bannerImage] => elect.jpg
[childCategories] => Array
(
Array
(
[categoryId] => 4
[title] => Smartphone
[bannerImage] => smartphone-banner.png
[childCategories] => Array
(
Array
(
[categoryId] => 4
[title] => Asus
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 5
[title] => Samsung
[bannerImage] =>
[childCategories] => Array
(
)
)
),
Array
(
[categoryId] => 6
[title] => Laptops
[bannerImage] =>
[childCategories] => Array
(
Array
(
[categoryId] => 7
[title] => HP
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 8
[title] => Dell
[bannerImage] =>
[childCategories] => Array
(
Array
(
[categoryId] => 9
[title] => Alienware Series
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 10
[title] => Vostro Series
[bannerImage] =>
[childCategories] => Array
(
)
)
),
)
)
)
)
)
は私のコードです: -
if($category->getData('level') > 2) {
$temp_array = array();
$paths = explode("/", $category->getData('path'));
for ($x=0; $x <= count($paths); $x++) {
if($x == 0 || $x == 1) {
unset($paths[$x]);
}
else {
$temp_array['categoryId'] = $category->getData('entity_id');
$temp_array['title'] = $category->getData('name');
$temp_array['bannerImage'] = $category->getData('image');
$temp_array['childCategories'] = array();
$temp_array['path'] = $category->getData('path');
$temp_array['parent'] = $category->getData('parent_id');
$temp_array['level'] = $category->getData('level');
$category_array[$category->getData('parent_id')]['childCategories'][$category->getData('entity_id')] = $temp_array;
}
}
}
注将来的に追加されるより下位カテゴリーとなりますがある場合は、こと。したがって、通常の配列割り当てはできません。
更新しますJSON形式の入力データを使用して3
{
"3": {
"categoryId": "3",
"title": "Electronic",
"bannerImage": "elect.jpg",
"childCategories": [],
"parent": "2",
"path": "1/2/3"
},
"4": {
"categoryId": "4",
"title": "Smartphone",
"bannerImage": "smartphone-banner.png",
"childCategories": [],
"parent": "3",
"path": "1/2/3/4"
},
"5": {
"categoryId": "5",
"title": "Laptop",
"bannerImage": null,
"childCategories": [],
"parent": "3",
"path": "1/2/3/5"
},
"6": {
"categoryId": "6",
"title": "Health",
"bannerImage": null,
"childCategories": [],
"parent": "2",
"path": "1/2/6"
},
"7": {
"categoryId": "7",
"title": "Vitamin",
"bannerImage": null,
"childCategories": [],
"parent": "6",
"path": "1/2/6/7"
},
"9": {
"categoryId": "9",
"title": "Fashion",
"bannerImage": null,
"childCategories": [],
"parent": "2",
"path": "1/2/9"
},
"10": {
"categoryId": "10",
"title": "For Her",
"bannerImage": null,
"childCategories": [],
"parent": "9",
"path": "1/2/9/10"
},
"11": {
"categoryId": "11",
"title": "For Him",
"bannerImage": null,
"childCategories": [],
"parent": "9",
"path": "1/2/9/11"
},
"12": {
"categoryId": "12",
"title": "Samsung",
"bannerImage": null,
"childCategories": [],
"parent": "4",
"path": "1/2/3/4/12"
},
"13": {
"categoryId": "13",
"title": "Dell",
"bannerImage": null,
"childCategories": [],
"parent": "5",
"path": "1/2/3/5/13"
},
"14": {
"categoryId": "14",
"title": "Alienware",
"bannerImage": null,
"childCategories": [],
"parent": "13",
"path": "1/2/3/5/13/14"
},
"15": {
"categoryId": "15",
"title": "Vostro",
"bannerImage": null,
"childCategories": [],
"parent": "13",
"path": "1/2/3/5/13/15"
},
"timestamp": "2017-10-31 04:20:20"
}
は、カテゴリがどのように見えるかの単なる視覚的な図です。 – N00bMagg
はい、わかりました。しかし、私があなたを手伝ってくれるようにしようとすると、ソリューションの開発とテストを行うために、サンプル入力の最小限/完全なセットを書く時間を費やす必要があります。 - これは、あなたの質問に魅力を感じさせない(ほとんどのボランティアに)。 – mickmackusa
@mickmackusa注目、あなたのためのjson形式を生成します。 – N00bMagg