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"?

1
  • Notice the different between $MAForJS and $MAtoJS (probably just typo). And you may need MAarray[2][0]; Commented Jul 17, 2019 at 5:59

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thats the correct way! JSON is valid JavaScript, no need to parse it really.
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]);

Comments

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

2 Comments

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.
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.
0

Try

var FirstName = MAarray[2][1];

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.