Mam tę tablicę
$a = array("008_@@_1_@@_Interieur_@@_1_@@_Inner-Bags", "008_@@_1_@@_Interieur_@@_2_@@_Color", "008_@@_1_@@_Interieur_@@_3_@@_Material");`
I używam funkcji rozbicia z @@ rozbijaj to. i otrzymuję tablicę w ten sposób ..
[0] => 008
[1] => 1
[2] => Interieur
[3] => 1
[4] => Inner-Bags
I tak dalej.
Więc chcę typ tablicy formatu.
array('008' => array( '1' => array('Interieur' => array('1' => 'Inner-Bags', '2' => 'Color', '3' => 'Material'))));
Taka jest moja logika...
<?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);
?>
Używam mojej logiki, ale nie otrzymałem odpowiedniej tablicy w ten sposób.
Array
(
[008] => Array
(
[1] => Array
(
[Interieur] => Array
(
[3] => Material
)
)
)
)
I chcę ustawić tablicę w tym formacie w ten sposób ..
Array
(
[008] => Array
(
[1] => Array
(
[Interieur] => Array
(
[1] => Inner-Bags
[2] => Material
[3] => Color
)
)
)
)
-4
Nishant Patel
25 wrzesień 2012, 12:46
2 odpowiedzi
Najlepsza odpowiedź
Spróbuj tego
$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 $val){
$a_exploded = explode("_@@_", $val);
$innerD[$a_exploded[0]][$a_exploded[1]][$a_exploded[2]][] = $a_exploded[3];
}
print_r($innerD);
2
Sibiraj PR
25 wrzesień 2012, 12:59
Ponieważ resetujesz tablicę wewnątrz pierwszego foreach
.
Zmiana
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];
}
Do:
foreach($a as $key => $val) {
$a_exploded = explode("_@@_", $a[$key]);
$innerA[$a_exploded[3]] = $a_exploded[4];
}
1
doktorgradus
25 wrzesień 2012, 12:57
Podobne pytania
Nowe pytania
php
PHP to szeroko stosowany, wysokopoziomowy, dynamiczny, zorientowany obiektowo i interpretowany język skryptowy przeznaczony głównie do tworzenia stron WWW po stronie serwera. Używane w przypadku pytań dotyczących języka PHP.