0

I have scripts like this.

javascript

$.ajax({
   url : "load_data_person.php", 
   dataType:"json",
   success:data_person(arrData)
});

function data_person(info_person){          
   var attr = [];
   var len = [];
   for(j=0;j<info_person.length;j++){
       attr.push(info_person[j][0]);    
       len.push(info_person[j][1]); 
   }
   return [attr, len];
}

How can I insert data to variable info_person like this:

info_person = [['fname',20],['lname',15],['addr',50]];

so I can get each value of attr and len?

Here is the script for data_person.php

<?php
$qStrPerson = mysql_query("SELECT atribut, len FROM tb_person ORDER BY fname ASC");
$arrFullPerson = array();
while($rStrPerson = mysql_fetch_array($qStrPerson)){
    $arrFullPerson[] = array($rStrPerson[atribut],$rStrPerson[len]);
}

echo json_encode($arrFullPerson);
// it will return like this : Array 0 : ['fname', 20], Array 1 : ['lname',15], Array 2 : ['addr',50]];
?>

Thank you for your help.

2
  • 1
    Post your json data. The one that you were getting Commented May 21, 2015 at 5:34
  • The info_person will contain data about more than one person data right.? Can you more clear on the question. Commented May 21, 2015 at 5:41

2 Answers 2

1

You can use simple jquery to convert the JSON to Javascript array

var array = JSON.parse(your json string);
Sign up to request clarification or add additional context in comments.

Comments

0

You can just format the array as you wanted in the server side and then echo it. While receiving the ajax response, you can simply

var info_person = json.parse(arData);

to convert the json-encoded value into javascript array.

Comments

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.