How can I add to an array of several objects?

0

I'm new to this and I'm trying to make a recursive function in php

public function recursive_tree($valor) {
    $consulta = $this->Modelo->Getdata('getHijos', $valor);
    if ($consulta->num_rows() > 0) {
        foreach ($consulta->result() as $p) {
            $arbol[] = array("id_user" => $p->id_user, "nombre" => $p->nombre, "id_raiz" => $p->id_raiz);
            $this->recursive_tree($p->id_user);
        }
        echo json_encode($arbol);
    }
}

,  I get all the data I need but the answer is

[{"id_user":"31","nombre":"lorena","id_raiz":"29"},
    {"id_user":"32","nombre":"pedro","id_raiz":"29"}]

[{"id_user":"33","nombre":"juan","id_raiz":"30"},
{"id_user":"34","nombre":"jose","id_raiz":"30"},],

as two different objects, what I try to obtain is to unite both.

[{"id_user":"31","nombre":"lorena","id_raiz":"29"},{"id_user":"32","nombre":"pedro","id_raiz":"29"},
{"id_user":"33","nombre":"juan","id_raiz":"30"},{"id_user":"34","nombre":"jose","id_raiz":"30"},]

I hope I expressed myself well and thank you for your help.

    
asked by juan guillermo moran leiva 27.11.2017 в 18:27
source

2 answers

1

It would be nice to see the contents of the table you are consulting. But the problem here is basically that every time you call recursive_tree you print the result instead of adding it to the same array.

You would say that you are calling the method twice, one passing it $valor 29 and another one with $valor 30. If your structure has a relationship of kinship:

- nodo 29
  - nodo 31
  - nodo 32
- nodo 30
  - nodo 33
  - nodo 34

Then it's natural to call

$instancia->recursive_tree(29);
$instancia->recursive_tree(30);

I will generate two arrangements for you.

Since get_recursive_tree is an instance method, you can access an instance property $arbol previously declared. Fill it with all the results you need, and only print the value at the end:

class miClase {
    $arbol = [];

    public function recursive_tree($valor) {
        $consulta = $this->Modelo->Getdata('getHijos', $valor);
        if ($consulta->num_rows() > 0) {
            foreach ($consulta->result() as $p) {
                $this->arbol[] = array("id_user" => $p->id_user, "nombre" => $p->nombre, "id_raiz" => $p->id_raiz);
                $this->recursive_tree($p->id_user);
            }
        }
    }

   public function imprime_arbol() {
      $this->recursive_tree(29);
      $this->recursive_tree(30);

     echo json_encode($this->arbol);

   }
}

Obviously it would be better if both 29 and 30 had a common father, or you could get all the parents from a previous consultation, so as not to manually call recursive_tree for every parent you know.

    
answered by 27.11.2017 в 19:55
-1

I hope this is what you're looking for friend

You can use the function concat()

The concat () method is used to join two or more fixes. This method does not change existing fixes, but returns a new fix.

Data in the arrangements you passed have a comma at the end you have to remove it

var objeto1 = [{"id_user":"33","nombre":"juan","id_raiz":"30"},{"id_user":"34","nombre":"jose","id_raiz":"30"}]

var objeto2 = [{"id_user":"33","nombre":"juan","id_raiz":"30"},{"id_user":"34","nombre":"jose","id_raiz":"30"}]

var objetoFinal =  objeto1.concat(objeto2);

$("#button").click(function(){
   $(".div").text(JSON.stringify(objetoFinal))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  
</div>
<button id="button" type="button" name="button">Objeto Unido</button>
    
answered by 27.11.2017 в 18:39