In practice, I create an array in PHP that then I pass to a JavaScript function.
So far so good. The array that the functions recieves is the following:
[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]
My problem is to get all the first array, that is this: {"firstname": "Micheal", "lastname": "Brown"}
and also the second array this: {"car": "Ford", "model": "Fiesta"}
I tried with alert (array [0])
but it only shows me this [
How can I do this?
I'll explain:
this is my php file:
class UserClasses
{
public $firstname;
public $lastname;
}
class CarClasses
{
public $car;
public $model;
}
if(isset($_POST['name'])){
populate($_POST['name']);
}
function populate(){
//I abbreviated the function for simplicity
$arrayUser = array();
$arrayCar = array();
$user = new UserClasses();
$user->firstname = "Micheal";
$user->lastname = "Brown";
array_push($arrayUser, $user);
$car = new CarClasses();
$car->car = "Ford";
$car->model = "Fiesta";
array_push($arrayCar, $car);
$arrayFinal = array($arrayUser, $arrayCar);
print json_encode($arrayFinal);
}
and this is the function in javascript:
//Ajax for calling php function
$.post('Classes.php', { name: name }, function(data){
var array = JSON.parse(data);
var arrayT = array[0];
alert(arrayT);
});