如何在php和smarty中制作多维数组
我有这个数组如何在php和smarty中制作多维数组,php,multidimensional-array,Php,Multidimensional Array,我有这个数组 $a = array("008_@@_1_@@_Interieur_@@_1_@@_Inner-Bags", "008_@@_1_@@_Interieur_@@_2_@@_Color", "008_@@_1_@@_Interieur_@@_3_@@_Material");` 我将explode函数用于@explode this。我得到的数组是这样的 [0] => 008 [1] => 1 [2] => Interieur [3] => 1 [4]
$a = array("008_@@_1_@@_Interieur_@@_1_@@_Inner-Bags", "008_@@_1_@@_Interieur_@@_2_@@_Color", "008_@@_1_@@_Interieur_@@_3_@@_Material");`
我将explode函数用于@explode this。我得到的数组是这样的
[0] => 008
[1] => 1
[2] => Interieur
[3] => 1
[4] => Inner-Bags
Array
(
[008] => Array
(
[1] => Array
(
[Interieur] => Array
(
[1] => Inner-Bags
[2] => Material
[3] => Color
)
)
)
)
foreach($a as $key => $val) {
$innerA = array(); // <-- it is wrong string. remove it.
$a_exploded = explode("_@@_", $a[$key]);
$innerA[$a_exploded[3]] = $a_exploded[4];
}
等等
所以我要的是格式数组的类型
array('008' => array( '1' => array('Interieur' => array('1' => 'Inner-Bags', '2' => 'Color', '3' => 'Material'))));
这是我的逻辑
<?php
$a = array("008_@@_1_@@_Interieur_@@_1_@@_Inner-Bags", "008_@@_1_@@_Interieur_@@_2_@@_Color", "008_@@_1_@@_Interieur_@@_3_@@_Material");
$i = 0;
echo '<pre>';
$innerD = array();
foreach($a as $key => $val) {
$innerA = array();
$a_exploded = explode("_@@_", $a[$key]);
$innerA[$a_exploded[3]] = $a_exploded[4];
}
foreach($a as $key => $val) {
//print_r($a[$key]);
$innerB = array();
$innerC = array();
$a_exploded = explode("_@@_", $a[$key]);
// print_r($innerA);
$innerB[$a_exploded[2]] = $innerA;
$innerC[$a_exploded[1]] = $innerB;
$innerD[$a_exploded[0]] = $innerC;
}
print_r($innerD);
?>
我想这样排列这个格式
[0] => 008
[1] => 1
[2] => Interieur
[3] => 1
[4] => Inner-Bags
Array
(
[008] => Array
(
[1] => Array
(
[Interieur] => Array
(
[1] => Inner-Bags
[2] => Material
[3] => Color
)
)
)
)
foreach($a as $key => $val) {
$innerA = array(); // <-- it is wrong string. remove it.
$a_exploded = explode("_@@_", $a[$key]);
$innerA[$a_exploded[3]] = $a_exploded[4];
}
因为您在第一个foreach中重置了数组 改变
foreach($a as $key => $val) {
$a_exploded = explode("_@@_", $a[$key]);
$innerA[$a_exploded[3]] = $a_exploded[4];
}
试试这个