I have a problem with the creation of a multidimensional array in JavaScript.
The PHP code looks like this:
<?php
$matches = array(1, 2, 3, 4, 4, 4, 6, 6, 2, 3);
foreach($matches as $match) {
$levels[$match][] = $match;
}
print_r($levels);
?>
print_r of $levels:
levels[1][0] = 1
levels[2][0] = 2
levels[3][0] = 3
levels[4][0] = 4
levels[4][1] = 4
levels[4][2] = 4
levels[6][0] = 6
levels[6][1] = 6
levels[2][1] = 2
levels[3][1] = 3
I have a problem with the creation the same array in JavaScript.
<script>
var levels = [];
$([1, 2, 3, 4, 4, 4, 6, 6, 2, 3]).each(function(key, value) {
levels[value][] = value;
});
</script>
Can someone help me create the same array in JavaScript?
JavaScript doesn't magically create an array if there is none. You have to create it yourself. So it would be something like
Learn more about arrays.