I use codeigniter for a webapp, i can get the data from my db into a array
我将codeigniter用于webapp,我可以将数据从我的数据库中获取到数组中
i have this multidimensional array
我有这个多维数组
Array
(
[0] => Array
(
[0] => Comerciales
[1] => Array
(
[0] => Terreno
[1] => Galera
[2] => Oficinas
[3] => Locales
[4] => Playas e Islas
[5] => Edificios
[6] => Hoteles
[7] => Propiedades de Inversión / Desarrollo
)
)
[1] => Array
(
[0] => Residenciales
[1] => Array
(
[0] => Array
(
[0] => Ciudad
[1] => Array
(
[0] => Terreno
[1] => Casa
[2] => Apartamento
)
)
[1] => Array
(
[0] => Montaña
[1] => Array
(
[0] => Terreno
[1] => Casa
[2] => Apartamento
)
)
)
)
)
I need to pass to a select2 data but de data i need is sonething like thad, i prove sone loop in php but i cant make it work. Please sorry for my english.
我需要传递给select2数据,但我需要的数据是像thad一样的声音,我在php中证明了sone循环,但我不能让它工作。抱歉我的英语。
var theData = [{
"id": "CEN",
"level": "C",
"dataid": "EN",
"text": "England",
"children": [{
"id": "RDEF",
"level": "R",
"dataid": "DEF",
"text": "Default Region",
"children": [{
"id": "D100",
"level": "D",
"dataid": "100",
"text": "Depot 100"
}, {
"id": "D125",
"level": "D",
"dataid": "125",
"text": "Depot 125"
}]
}, {
"id": "RNORTH",
"level": "R",
"dataid": "NORTH",
"text": "North Region",
"children": [{
"id": "D999",
"level": "D",
"dataid": "999",
"text": "Depot 999 - Head Office"
}]
}, {
"id": "RWEST",
"level": "R",
"dataid": "WEST",
"text": "West Region",
"children": [{
"id": "D555",
"level": "D",
"dataid": "555",
"text": "Depot 555"
}]
}]
}, {
"id": "CNL",
"level": "C",
"dataid": "NL",
"text": "Netherlands",
"children": [{
"id": "RNORTH",
"level": "R",
"dataid": "NORTH",
"text": "North Region",
"children": [{
"id": "DNL",
"level": "D",
"dataid": "NL",
"text": "Dutch Depot"
}
]
}]
}]
1 个解决方案
#1
1
this is what y do, i hope this help, if sone one can doit better please doit
这就是你做的,我希望这有帮助,如果一个人可以做得更好,请做
[<?php foreach ($categorias as $categoria){
foreach ($categoria as $key=>$parent) {
if ($key==0) {
?>{
"id": "<?php echo $parent['categoriaid']; ?>",
"level": "<?php echo $parent['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $parent['nombre']; ?>"
<?php }else {?>
,"children": [<?php
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($parent);
$last_row = end($gdjci);
foreach ($parent as $key=>$children) {
if (array_key_exists('nombre', $children)) {?>
{
"id": "<?php echo $children['categoriaid']; ?>",
"level": "<?php echo $children['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $children['nombre']; ?>"}
<?php
if ($key == $last_row) {
// last element
echo "]},";
} else {
// not last element
echo ",";
}
} else {
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($children);
$last_row = end($gdjci);
foreach ($children as $key=>$parent) {
if ($key==0) {
?>{
"id": "<?php echo $parent['categoriaid']; ?>",
"level": "<?php echo $parent['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $parent['nombre']; ?>"
<?php
if ($key == $last_row) {
// last element
echo "]";
} else {
// not last element
echo ",";
}
}else {
?>
"children": [
<?php
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($parent);
$last_row = end($gdjci);
foreach ($parent as $key=>$children) {
if (array_key_exists('nombre', $children)) {
?>{
"id": "<?php echo $children['categoriaid']; ?>",
"level": "<?php echo $children['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $children['nombre']; ?>"}
<?php
if ($key == $last_row) {
// last element
echo "]},";
} else {
// not last element
echo ",";
}
} else {
}
}
}
}
}
}
}
}
}
echo ']';
echo '}]';
#1
1
this is what y do, i hope this help, if sone one can doit better please doit
这就是你做的,我希望这有帮助,如果一个人可以做得更好,请做
[<?php foreach ($categorias as $categoria){
foreach ($categoria as $key=>$parent) {
if ($key==0) {
?>{
"id": "<?php echo $parent['categoriaid']; ?>",
"level": "<?php echo $parent['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $parent['nombre']; ?>"
<?php }else {?>
,"children": [<?php
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($parent);
$last_row = end($gdjci);
foreach ($parent as $key=>$children) {
if (array_key_exists('nombre', $children)) {?>
{
"id": "<?php echo $children['categoriaid']; ?>",
"level": "<?php echo $children['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $children['nombre']; ?>"}
<?php
if ($key == $last_row) {
// last element
echo "]},";
} else {
// not last element
echo ",";
}
} else {
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($children);
$last_row = end($gdjci);
foreach ($children as $key=>$parent) {
if ($key==0) {
?>{
"id": "<?php echo $parent['categoriaid']; ?>",
"level": "<?php echo $parent['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $parent['nombre']; ?>"
<?php
if ($key == $last_row) {
// last element
echo "]";
} else {
// not last element
echo ",";
}
}else {
?>
"children": [
<?php
//necesario para colocar algo al final de cada registro y algo diferente en el ultimo
$gdjci = array_keys($parent);
$last_row = end($gdjci);
foreach ($parent as $key=>$children) {
if (array_key_exists('nombre', $children)) {
?>{
"id": "<?php echo $children['categoriaid']; ?>",
"level": "<?php echo $children['parentid']; ?>",
"dataid": "<?php echo $key; ?>",
"text": "<?php echo $children['nombre']; ?>"}
<?php
if ($key == $last_row) {
// last element
echo "]},";
} else {
// not last element
echo ",";
}
} else {
}
}
}
}
}
}
}
}
}
echo ']';
echo '}]';