0

I use codeigniter for a webapp, i can get the data from my db into a array

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.

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"
    }
                    ]
    }]
}]
7
  • what code have you tried? Commented Mar 6, 2014 at 16:19
  • var tempArray = <?php echo json_encode($categorias); ?>; alert(tempArray[0].Key); Commented Mar 6, 2014 at 16:23
  • are you getting any error when you run this? if so , what is the yo error you are getting? it would be mighty helpful if you could paste it here? Commented Mar 6, 2014 at 16:27
  • is not just the error, is not what i need, i need to transform the first array into the second or something close Commented Mar 6, 2014 at 16:37
  • if you want to do that there should be some relation between the two arrays, which i'm not able to see. so if you can explain that , we can get somewhere Commented Mar 6, 2014 at 16:42

1 Answer 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 '}]'; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.