How to get multiple arrays with 1 single index and put in a single array?

1

I am 3 days trying to solve a problem, I can not get multiple arrays with 1 single index and put and a single array.

EX:
Está imprimindo assim:
Array(
      [0]=> "MT"
     )
Array(
      [0]=> "MG"
     )
Array(
      [0]=> "AM"
     )

Quero fazer assim:

Array([0]=>"MT", [1]=>"MT", [2]=>"MT")


//MEU BANCO DE DADOS CONSIDERANDO APENAS AS PESSOAS DA REDE DO ID 1001

id_usuario |  uf
-------------------------
1001          |  SP
1003          | MG
1005          | AM
1027          | MT

// MONTAGEM DA REDE

                 1001
                /    \
             1003     1005
              /
           1027

My code:

<?php 
function buscarEstados1($id){
    include 'includes/conexao.php'; 

    $s_down = "select id from tab_afiliado where upline = $id";
    $res_down = $cnns->query($s_down);
    foreach ($res_down as $down){
        $aff_uf = $down['id'];//var_dump($aff_uf); //PEGA OS IDS 1003, 1027, 1005

        $s_uf = "select uf,id_endereco from tab_endereco WHERE id_endereco = " . $aff_uf . "";
        $res_uf = $cnns->query($s_uf);
        foreach ($res_uf as $uf){
            $arr = $uf['uf']; //var_dump($arr); //PEGA AS UF MG , AM , MT 
        }

        buscarEstados1($aff_uf);
    }
}

    buscarEstados1(1001);
?> 

I would like to take the UF and make an array only Array ( "MG" , "AM" , "MT");

The problem is as if the loop caught like this.

 print_r(#arr);

    $arr = array( "MG" );
    $arr = array( "MT" ); 
    $arr = array( "AM" );
    
asked by anonymous 08.01.2016 / 13:25

3 answers

1

Create two functions to make logic easier, one to fetch all affiliates and one to fetch their status.

function buscarAfiliados($id){
    include 'includes/conexao.php'; 

    $s_down = "select id from tab_afiliado where upline = $id";
    $res_down = $cnns->query($s_down);
    foreach ($res_down as $down){
     $aff_uf = $down['id'];

     return $cnns->query($s_uf);
    } 
}

function buscarEstados($id){
    include 'includes/conexao.php'; 
    $s_uf = "select uf,id_endereco from tab_endereco WHERE id_endereco = " . $id . "";
    $res_uf = $cnns->query($s_uf);
    foreach ($res_uf as $uf){
        $arr[] = $uf['uf']; 
    }
    return $arr;
}

    $afiliados = buscarAfiliados(1001);

    foreach($afiliados as $item){
        $estados[] =buscarEstados($item['uf'])
    }
    
08.01.2016 / 13:34
1

I would not do so, but giving you an alternative would be:

<?php 
function buscarEstados1($id, &$arr = array()){
    include 'includes/conexao.php'; 

    $s_down = "select id from tab_afiliado where upline = $id";
    $res_down = $cnns->query($s_down);
    foreach ($res_down as $down){
        $aff_uf = $down['id'];//var_dump($aff_uf); //PEGA OS IDS 1003, 1027, 1005

        $s_uf = "select uf,id_endereco from tab_endereco WHERE id_endereco = " . $aff_uf . "";
        $res_uf = $cnns->query($s_uf);
        foreach ($res_uf as $uf){
            $arr[] = $uf['uf']; //var_dump($arr); //PEGA AS UF MG , AM , MT 
        }

        buscarEstados1($aff_uf, $arr);
    }
}

    $ufs = array();
    buscarEstados1(1001, $ufs);

    print_r($ufs); // Seu Resultado
?> 
    
08.01.2016 / 15:06
0

Make a loop by getting the multidimensional array values and throwing them into a simple array.

The example below overwrites the original array itself:

$arr = array(array('a'), array('b'), array('c'));

foreach ($arr as $k => $v)
    $arr[$k] = $v[0];

print_r($arr);

If you want to modify the structure of the SQL query, using JOIN, see example below

    <?php 
    function buscarEstados1($id){
        include 'includes/conexao.php'; 


        $sql = "select 
        T1.id AS id,
        T3.id AS afiliado_id,
        T2.uf AS uf, 
        T2.id_endereco AS id_endereco
        from tab_afiliado AS T1
        LEFT JOIN tab_afiliado AS T3 ON T3.upline = T1.id
        LEFT JOIN tab_endereco AS T2 ON T2.id_endereco = T1.id"
        where upline = $id";

        $rs = $cnns->query($sql);

        print_r($rs); exit;

/**
O restante do código omiti pois não entendi porque está fazendo recursividade.
*/
    }
    
08.01.2016 / 13:34