1

in php I have this array

$MAtoJS[] = array('Max','Mustermann','N','A80');
$MAtoJS[] = array('Michaela','May','N','A78');
$MAtoJS[] = array('Hans','Gerstelhuber','N','M12');
$MAtoJS[] = array('Alfred E.','Neumann','N','T25');
$MAtoJS[] = array('James','Bond','N','M72');

Still in the php part I prepare the array for js

$MAarrayForJS = json_encode(json_encode($MAForJS));

In the javascript part I create a js-array

var MAarray = new Array(<?php echo $MAarrayForJS; ?>); alert(MAarray)

The content of MAarray is

[["Max","Mustermann","N","A80"],["Michaela","May","N","A78"],["Hans","Gerstelhuber","N","M12"],["Alfred E.","Neumann","N","T25"],["James","Bond","N","M72"]]

using

console.log(MAarray[0]);

I tried to get for example the first name of Hans by this code

var FirstName = MAarray[0][2][1];

which results in "undefined" in the console.log.

How can I access to a specific value in a specific array, in this case to set the var FirstName to the value "Hans" from the third "subarray"?

  • Notice the different between $MAForJS and $MAtoJS (probably just typo). And you may need MAarray[2][0]; – dWinder 15 hours ago
1

Are you looking for this... ?

$MAtoJS = [];

$MAtoJS[] = array('Max','Mustermann','N','A80');
$MAtoJS[] = array('Michaela','May','N','A78');
$MAtoJS[] = array('Hans','Gerstelhuber','N','M12');
$MAtoJS[] = array('Alfred E.','Neumann','N','T25');
$MAtoJS[] = array('James','Bond','N','M72');

$MAarrayForJS = json_encode($MAtoJS);

?>

<script>
    var MAarray = <?php echo $MAarrayForJS; ?>;
    console.log(MAarray[0][0]); // Max
</script>
  • Thats the correct way! JSON is valid JavaScript, no need to parse it really. – Manuel Mannhardt 13 hours ago
1

Please note that the array is 2 dimensional and not 3 dimensional you're accessing the 3rd dimension which will always yield undefined, have a look at below code snippet in which the value is fetched, altered as per the requirement:

let MAarray = [["Max","Mustermann","N","A80"],["Michaela","May","N","A78"],["Hans","Gerstelhuber","N","M12"],["Alfred E.","Neumann","N","T25"],["James","Bond","N","M72"]];

console.log(MAarray[2][0]);

MAarray[2][0] = "New Name";

console.log(MAarray[2][0]);

0

For some reason you are using json_encode 2 times. You only need to use it once.

Change following

$MAarrayForJS = json_encode(json_encode($MAForJS));

To

$MAarrayForJS = json_encode($MAtoJS);

Then in the JS you can use console.log(MAarray[0][2][1]);

  • Ok, thanks. Works now as wanted. The reason is "google" - how to determine which search result is right? And there were many with double json_encode. – Christian 15 hours ago
  • I have never used double json_encode and neither came across a reason to do so. No way to determine which google result is correct, just need to play around the result. – user1309690 15 hours ago
0

Try

var FirstName = MAarray[2][1];

It seems like you are trying to access the third level of a two dimensional array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.