PHP Javascript - Array multidimensional error

php multidimensional array foreach
php multidimensional array with keys
json multidimensional array example php
php json_encode multidimensional array
multidimensional array javascript
multidimensional array php
create multidimensional json array in php
php add to multidimensional array

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;


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

Here's what is happening with your code: you're accessing the first element of a JSON string, so my guess is you will get its first character: [.

You need to convert your string into an actual array before accessing it!

Use JSON.parse:

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'

// that's what you have

// that's what you want
array = JSON.parse(json)

Passing Multidimensional Arrays from PHP to JavaScript using JSON, Passing Multidimensional Arrays from PHP to JavaScript JSON_PRETTY_PRINT not used since it would trigger error with JSON.parse /* output manually  The main thing to understand here is that you cannot treat an array like a string. If you attempt to do so, PHP will display a notice. Multidimensional arrays. Multidimensional arrays can also cause problems if you are not careful. Take the following example:

First of all, if you are using json_encode to pass PHP array to JS, it encodes it to a JSON string. You have to parse it using JSON.parse() and only then the json string is transformed to regular array which you can use.

PHP Multidimensional Arrays, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  php,arrays,multidimensional-array, array_unshift not working on a multidimensional array, PHP [closed] The error says that the first argument has to be array, not an array element.

You can parse data and use it:

    var arr = '<?php echo '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'; ?>';
    var parsedarr =  JSON.parse(arr);

    for(var i=0;i<parsedarr.length;i++){

Check if an Item exists in a multidimensional Array - JavaScript, The problem with it is that if the item is not found it results in an error and returns the index of an Item if it is found in a multidimensional array. PHP - Multidimensional Arrays A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.

Multidimensional arrays in PHP, In other words, define multi-dimensional arrays as array of arrays. As the Two dimensional array: It is the simplest form of a multidimensional array. array in JavaScript · How to search by key=>value in a multidimensional array in PHP ? For this reason, we can say that a JavaScript multidimensional array is an array of arrays. The easiest way to define a multidimensional array is to use the array literal notation. To declare an empty multidimensional array, you use the same syntax as declaring one-dimensional array: let activities = [];

Multidimensional array in JavaScript, How to display error without alert box using JavaScript ? How to get all ID of the DOM elements with JavaScript ? How to create a Ripple Effect on Click the Button  Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions. Dimensions: Dimensions of multidimensional array indicates the number of indices needed to select an element. For a two dimensional array two indices to select an element. Two dimensional array: It is the simplest form of a multidimensional array. It can be created using nested array.

print_r - Manual, If given an array, values will be presented in a format that shows keys and elements. print the javascript function toggleDisplay() and then the transformed output Heres a simple fix for anyone who is having the same problem as I did. Multidimensional arrays are not directly provided in JavaScript. If we want to use anything which acts as a multidimensional array then we need to create a multidimensional array by using another one-dimensional array. So multidimensional arrays in JavaScript is known as arrays inside another array.

Indexed collections, The Array object has methods for manipulating arrays in various ways, such Using this characteristic of JavaScript arrays, multi-dimensional  array_push() trata array como si fuera una pila y coloca la variable que se le proporciona al final del array.El tamaño del array será incrementado por el número de variables insertados.

  • You should learn the difference between JSON and an actual array.
  • Add your JavaScript function to the question please. Your problem is, that your JSON is interpreted as a string in JavaScript, instead of an array of objects. To help you solve the problem we need a glimpse at your JS code.
  • you're accessing the first element of a JSON object -> You're accessing the first element of a JSON string thus returning the first character.
  • I tried but give me this now: [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object] , I modified the post by adding my code
  • Of course, you're are using alert, it will never show you the actual object, just [object Object]. Always use console.log to debug your code!!!
  • Great Stefano, Don't forget to upvote / accept answer(s) you felt helped solve you problem for future reference.