I'm struggling to extract data from a multidimensional array.
In my .php file, I retrieve data from a database and encode it to JSON.
JSON= {"1":{"SME":"0","SAUDE":"0"}....
Desired data structure:
array{
0 => Array{"SME" => 1,
"SAUDE" =>4}
1 => Array{"SME" => 10,
"SAUDE" => 0}
}
In my .HTML:
$.getJSON('getOrgaoAno.php', function(data) {
$.each(data, function(key, val) {
alert(key); // Displaying the index of the first array
alert(val); // Displaying [OBJECT] in the alert box
}
});
How can I access the data from the second array and store it in an array in my .HTML for use in Chart.js?
UPDATE 1
Here's the data I'm receiving from the .php encoded to JSON (using console.log now, which is much easier):
The "1,2,3....9" represents the keys of the first array containing objects:
1
Object { SECRETARIA MUNICIPAL DE DESENVOLVIMENTO URBANO : "0", SECRETARIA MUNICIPAL DE SAUDE: "0", SETOR DE RH: "0", SECRETARIA MUNICIPAL DE CULTURA, TURISMO, ESPORTE E LAZER : "0", SETOR DE CADASTRO DE IMOVEIS: "0", SECRETARIA MUNICIPAL DE DESENVOLVIMENTO ECONÔMICO, TRABALHO E MEIO AMBIENTE: "0", SETOR AGILIZA: "0" }testedados.html:21:5
2
Object { SECRETARIA MUNICIPAL DESENVOLVIMENTO URBANO : "1", SECRETARIA MUNICIPAL DE SAUDE: "0", SETOR DE RH: "0", SECRETARIA MUNICIPAL DE CULTURA, TURISMO, ESPORTE E LAZER : "0", SETOR DE CADASTRO DE IMOVEIS: "0", SECRETARIA MUNICIPAL DE DESENVOLVIMENTO ECONÔMICO, TRABALHO E MEIO AMBIENTE: "0", SETOR AGILIZA: "0" }testedados.html:21:5
3
Object { SECRETARIA MUNICIPAL DE DESENVOLVIMENTO URBANO : "0", SECRETARIA MUNICIPAL DE SAUDE: "1", SETOR DE RH: "1", SECRETARIA MUNICIPAL DE CULTURA, TURISMO, ESPORTE E LAZER : "0", SETOR DE CADASTRO DE IMOVEIS: "0", SECRETARIA MUNICIPAL DE DESENVOLVIMENTO ECONÔMICO, TRABALHO E MEIO AMBIENTE: "0", SETOR AGILIZA: "0" }testedados.html:21:5
4
Object { SECRETARIA MUNICIPAL DE DESENVOLVIMENTO URBANO : "0", SECRETARIA MUNICIPAL...
I want to extract the "keys" and "values" from the object and store them in an array.
For example:
var Desc = "Key"
var Valor = "Value"
Or should I consider changing the way I retrieve data from .php?
UPDATE 2
To select values in an array, you can use:
Two "Jquery.each" loops:
var index = 0;
var index2 = 0;
jQuery.each( data, function( key, value ) {
index++;
index2 = 0;
jQuery.each( value, function( key, value ) {
if (index2 == 0)
{
arrDesc[index] = key;
arrDesc[index] += ",";
arrQtd[index] = value;
index2 ++;
}
else
{
arrDesc[index] += key;
arrDesc[index] += ",";
arrQtd[index] += value;
}
});
});