Creating multidimensional array from query to BD MySQL

1

I have the following problem:

I have a table in a MySQL database and in a code snippet I need to make content retrieved by a query made to this database be stored in a multidimensional array / array in php. The values of the two columns queried are related and I can not imagine in any other way to maintain the relationship between items in a practical way without being stored in a structure variable similar to the database table (in this case, the multidimensional array) .

I want to store the contents of two columns of a SQL table in a multidimensional array, so that I can access the array with

'array["valor1_da_tabela"]= "valor1,valor2,valor3,..."
 array["valor2_da_tabela"]= "valor1,valor2,valor3,..."
 array["valor3_da_tabela"]= "valor1,valor2,valor3,..." 
'

Detail: Solution suggestions should be made using PDO. All of this is to be able to do a cascaded javascript combobox / dropdown scheme on a portal page. And the items in these combobox / dropdown are all values stored in this SQL table. The first one works quietly. The second one that depends on what was selected in the first one is not leaving due to the execution order of the client and server side codes. I need to store these two columns with string values to make the implementation of the dynamic cascade combobox / dropdown implementation easier. If you have suggestions on that, I also thank you.

Thank you for your attention.

EDIT: This is an excerpt from a part of the project that does something similar only for just one column. What I've tried to do is adapt this snippet to meet my need to create a multidimensional array rather than an ordinary array.

<?php 
    ini_set('default_charset','UTF-8');
    require("../dbconnect.inc.php");
    $vet = array();
    $assunto = array();
    $jsArray = "";
    $sql = "SELECT distinct assunto
                FROM arquivos";

    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $vet = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach($vet as $vet_assunto){
    array_push($assunto, $vet_assunto['assunto']);
}       
    for($i=0;$i<sizeof($assunto);$i++){
    $jsArray = $jsArray . $assunto[$i] . ",";
    }
    $jsArray = chop($jsArray, ","); 
    require("../dbdisconnect.inc.php");     
?>

<script type="text/javascript" language="javascript">

    var jsStrArray = "<?php echo $jsArray; ?>";
    var listaAs = jsStrArray.split(",");

        $(document).ready(function() {     

            $.each(listaAs, function(i, val){
            var content = val;          
            $('#painel').append('<a href="aulaView.php?assunto='
                                + content   
                                + '"><div class="tile"><p class="asTitle"><b>' 
                                + content 
                                +'</b></p></div></a>');
            });

           $(".tile").hover( function(){
                $(this).css({'border':'3px solid #007CFF','box-shadow':'2px 2px 7px #00B2FF'});
           }, 
            function(){
                $(this).css({'border':'3px solid #151515','box-shadow':'2px 2px 7px #000000'});
            });
        });
</script>

This snippet is one of the attempts:

        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $vet = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach($vet as $vet_assunto){
        array_push($assunto['assunto'], $vet_assunto['assunto']);   
        array_push($assunto['assunto']['aula'], $vet_assunto['aula']);  
    }

        require("../dbdisconnect.inc.php");     
    ?>

Example of the two columns in question in the database table:

Assunto|aula

teste1 | 1
teste1 | 2
teste2 | 1
teste2 | 2
teste2 | 3

And I would like the array to be organized as follows:

                  Output
   array[teste1]= {1,2}
   array[teste2]= {1,2,3}

array[teste1][1]= {2}
array[teste2][2]= {3}
    
asked by anonymous 13.11.2015 / 15:25

0 answers