How to take several arrays with 1 single index and put in a single array?

Asked

Viewed 786 times

1

I’m 3 days trying to solve a problem, I can’t get multiple arrays with 1 single input 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 Ufs 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" );
  • try to fix this when mounting the array. If it is not feasible you will have to do a basic POG. (Gambiarra Oriented Programming)

  • These values come from where of the bank? it is not very clear what is the problem.

  • Pq the function is called (end of the first foreach) and returned nothing?

  • include should not be within a recursive function, put it at the beginning of the file will avoid many headaches. php connection.

  • This result happens pq 1 - $arr has its superscript value every turn of the foreach, 2 - the function does not return value and is called again.

  • You can mark the answer that most helped you to solve the problem (Guilherme Lautert) as accepted, it gets a green light, or create your own response and explain the details. See => http://meta.pt.stackoverflow.com/a/1079/91

Show 1 more comment

3 answers

1

Create two functions to facilitate logic, one to search for all affiliates and the other to search for 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'])
    }
  • rray didn’t work pq my loop is taking the arrays as if they were all the same with Dice [0] Like this: $arr = array( "MG" ); print_r($arr); $arr = array( "MT" ); print_r($arr); $arr = array( "AM" ); print_r($arr);

  • @clicabarato I still can’t understand ...

  • As if the loop caught it like this... $arr = array('MG'); $arr = array('MT'); $arr = array('AM');

  • When I give a print_r($arr) it shows so MG MT AM... Each with the same array and Indice [0]

  • That’s the goal! to get the values can use a foreach or implode() if you want to format as a string.

  • @clicabarato I think you need to edit the question and add more details if you read the first sentence of the question and that comment looks like it’s been resolved.

  • @click a Join wouldn’t solve this problem? instead of doing two foreach?

  • It would not solve pq the big problem is to have to take the BD line by line each person that is below their respective Uplines in each of their own networks.... otherwise what I want to do would be very simple using DISTINCT itself

  • first I need to take the main id of the network and then take your 2 Downllines and so on and see no other way

  • Thanks rray, I will find interesting these function organizations because I am starting now in programming, I hope one day to get to be a beast like you !....= D

Show 5 more comments

1

I wouldn’t do it like this, 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
?> 
  • 1

    Guilherme Thanks your tbm worked, just missed to return in the recursive of the function lookup1($aff_uf, $arr);

  • I’m gonna use your code 'cause it got shorter, thank you very much!!!

0

Loop by taking the values of the multidimensional array and throwing them into a simple array.

In the example below override 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.
*/
    }
  • Daniel actually looks like this: $arr = array('a'); $arr = array('b'); $arr = array('c'); ... So I’m not getting already I changed the position in all ways but always shows so =(

  • 1

    Edit your question as this changes the answer quite a bit. It is unclear what the first array looks like

  • Daniel take that code I just edited and print it out so you can see what the code is really doing

  • I don’t understand where you’ve modified it. The problem is understanding how the first array that contains is mounted Array(&#xA; [0]=> "MT"&#xA; ), got it? That first array is like this? array(array('a'), array('b'), array('c')); or so array(''a); array('b'); array('c') Is it a multidimensional array or is it 3 arrays?

  • are 3 arrays, because I was not able to put it in 1 array only

  • 1

    got it. vc can solve several ways.. one way is to make a single SQL query using JOIN.. you really want to do these tricks with array or want to improve the query? I suggest the query

  • How would I do this JOIN? Whereas I only need to pull people from their respective networks?

  • I added Join’s suggestion to the answer.. but now I realize that you make a recursive call buscarEstados1(). Why do you need this?

  • I recur to return the id of the next network that will enter below it more 2 ids and so on always returning the id of the next network, because it first searches the left side and then the right side with this in the design of the network that I did there in the question

  • I got it! This needs something more sophisticated. It’s completely out of context. You have two options which is to completely change the question or continue in the gambiarra with arrays.

  • opaaa...ideas are always very welcome.... What’s the idea?

  • was seeing here the JOIN and it takes only the 2 indicated of the first of the network, then it would need to take the indicated of this 2 and so on

  • did Join before you talk about this pyramid scheme.. This looks like pyramid/network system, no?

  • I think you’ll need a lot of remodeling if you want to do something more efficient. Note that in id_address you’re using the same affiliate id. The ideal would be to have an affiliate id reference field only because imagine when you need to add more than one address per affiliate. There is already a complication and will probably solve with gambiarra in the database and it will turn into a snowball. You leave to "tidy up later" but in fact never arrruma rsrs. So I suggest redoing the modeling. Better to suffer now at the beginning when it’s small than to see a nightmare with this already big.

  • modified the query. Run there and see the result. I added the relationship with the same table , returning the network ids. This way you don’t need to do this recursion and will greatly reduce code and processing.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.