1

I'm having trouble getting the data from a multidimensional array.

I have a .php file that get the data from database and encode to JSON

JSON= {"1":{"SME":"0","SAUDE":"0"}....

Example of what I'm expecting:

     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); // Show me the index of the first array
      alert(val); //Show me [OBJECT] in the alert box
    }
});

How can I get the data from the second array and put into an array in my .HTML to use in Chart.js?

UPDATE 1

What i´m getting from the .php encoded to JSON ( now i´m using console.log thx, it´s a lot easier)

The "1,2,3....9" is the "key" of the first array that contains the object

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 DE 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 DE SAUDE: "0", SETOR DE RH: "0", SECRETARIA MUNICIPAL DE CULTURA, TURISMO, ESPORTE E LAZER : "1", SETOR DE CADASTRO DE IMOVEIS: "0", SECRETARIA MUNICIPAL DE DESENVOLVIMENTO ECONÔMICO, TRABALHO E MEIO AMBIENTE: "0", SETOR AGILIZA: "0" }testedados.html:21:5
5
Object { SECRETARIA MUNICIPAL DE DESENVOLVIMENTO URBANO : "0", SECRETARIA MUNICIPAL DE SAUDE: "0", SETOR DE RH: "0", SECRETARIA MUNICIPAL DE CULTURA, TURISMO, ESPORTE E LAZER : "1", SETOR DE CADASTRO DE IMOVEIS: "0", SECRETARIA MUNICIPAL DE DESENVOLVIMENTO ECONÔMICO, TRABALHO E MEIO AMBIENTE: "1", SETOR AGILIZA: "0" }testedados.html:21:5
6
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
7
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: "1", SETOR AGILIZA: "0" }testedados.html:21:5
8
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: "1", SETOR AGILIZA: "0" }testedados.html:21:5
9
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" }

So, i want to split in a array the "key" and the "value" of the object

Like that if possible

var Desc = "Key"

var Valor = "Value"

Or it´s better to change the way i´m getting the data from .php?

UPDATE 2

For selecting the values in a array use

Two "Jquery.each"

    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;
         }

      });
    });
1
  • are you not getting the json back from the php file? I read it as if you are if you are encoding the array into json. If you are able to get the json, and print the key value pair, then what exactly is your issue? Commented Sep 6, 2016 at 20:52

2 Answers 2

4

Try to debug your code with console.log() instead of alerts. Then you will see the following (open the browsers console with F12. To push the inner object to an array, you can just use Array.prototype.push().

var j = {"1":{"SME":"0","SAUDE":"0"}};
var arr = [];

$.each(j, function(key, val) {
    console.log(key); // 1
    console.log(val); // Object {SME: "0", SAUDE: "0"}
    console.log(val.SAUDE); // 0
    console.log(val.SME); // 0

    // add the object to the array:
    arr.push(val);     

});
Sign up to request clarification or add additional context in comments.

1 Comment

Sry for late response but bolydays... How can i get the value of ".val.SAUDE" without "SAUDE" because i get it from a data base and i don´t know what will be the name of the fields I have updated my question for a better understanding
0

Your PHP script "getOrgaoAno.php" should like as below

<?php
 $data = array(/*YOUR DATA IS HERE*/);
 echo json_encode($data);
?>

No more extra spaces or any characters in the "getOrgaoAno.php" script before "<?php" and after "?>", Otherwise JSON format error in JavaScript

And your JavaScript is ok, Just console from browser.

<script type="text/javascript">
 $.getJSON('getOrgaoAno.php', function(data) {
  $.each(data, function(key, val) {
   console.log(key); //Show me the index of the first array
   console.log(val); //Show me [OBJECT] in the console
  })
 });
</script>

2 Comments

Sry, for late response, but holydays... console.log is a lot easier thx
console.log is a lot easier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.