2

I have the following php code that gives an array variable called "markers".

window.markers = [];

<?php if( have_rows('actin_center') ): ?>
<?php while( have_rows('actin_center') ): the_row(); ?>

window.markers.push( [ ['<?php the_sub_field('center_name'); ?>', '<?php the_sub_field('center_address'); ?>', <?php the_sub_field('latitude'); ?>, <?php the_sub_field('longitude'); ?>] ] );

<?php endwhile; ?>
<?php endif; ?>

This works fine so far yet returns the array (on alert) as:

Cool center 1,Rewitz Gofella, 1234 Lorem,50,50,Cool center 2,Lorem Ipsum, 1234 Quosque,60,60,Cool center 3,Veniat elaborat, 1234 Ipsum,70,70

What I need, yet, is the following form keeping the arrays (of sub_fields) as they originally are inside the array and NOT concatenate them. As:

var markers = [
    ['First Center','First Address',50,50],
    ['Second Center','Second Address', -25.363882,131.044922],
    ['Third Center','Third Address', 10.363882,95],
    ['Fourth Center','Fourth Address', -50,-90],
    ['Fifth Center','Fifth Address', 30,5],
];

As you can see in the code above I tried with the simple double bracket [[ ]] but this doesn’t work. How is this to be done correctly? Thanks so much for help.

PS: If someone feels urged to down vote my question, please be so kind to let me know why so I may learn something.

5
  • Post your expected output along and input Commented Jun 15, 2015 at 6:23
  • 3
    You see the array concatenated because you are alerting the array, which when done calls the toString() function which joins the array. Try using console.log(window.markers) and look in the console you should see the array structure. Commented Jun 15, 2015 at 6:24
  • 1
    You don't need double brackets. Single bracket is a proper use: window.markers.push(['arrayItem1', 'arrayItem2']);. Check your output by using console.log(window.markers). Commented Jun 15, 2015 at 6:24
  • 2
    Or, if you'd still prefer to use alert, do alert(JSON.stringify(window.markers)); to see the array structure. Commented Jun 15, 2015 at 6:25
  • You are all awesomely right! Wow, learned something today. In fact my code did work all the time, only that I stopped finishing it because of the "wrong" alert message! THANKS!! Commented Jun 15, 2015 at 8:14

1 Answer 1

1

Due to comments:
alert( [[1,2][3,4]] ) will popup wrong 1,2,3,4
alert( JSON.stringify([[1,2][3,4]]) will popup [[1,2],[3,4]]

.push([1,2]) will add an array to markers: [[1,2],[3,4],[5,6]]
.push(1,2) will add elements to markers: [1,2,3,4,5,6]

But better way, is don't execute javascript .push (save client CPU time)
Define array in javascript in this way:

window.markers = [
<?php while( have_rows('actin_center') ): the_row(); ?>
  ["<?php the_sub_field('center_name');?>","<?php the_sub_field('center_address'); ?>",<?php the_sub_field('latitude');?>, <?php the_sub_field('longitude');?>],
<?php endwhile; ?>
];

result should looks like this

window.markers = [
['First Center', 'First Address', 50, 50],
['Second Center','Second Address',-25.363882, 131.044922],
[... ,... , ... ,...],
];
Sign up to request clarification or add additional context in comments.

5 Comments

you can insert some more brackets, to separate data sets; direct after while (...) and before endwhile. the last comma does not matter.
The result of the originally posted code in this answer is not what I wanted. I need the bracketed arrays as data sets. @Nina: Guess that is what I need here, but I am afraid to not understand where the brackets go exactly. By the way: Why is the push method not good? Because it gves me the desired output.
@Garavani datasets meaning u want {'center_name': 'First center', 'center_address': 'First Address', ..}? Push is bad because its need to be executed. Why do u need this if u can create plain script to save some client CPU activity? Show exactly what u need.
@Garavani: here ... <?php while( have_rows('actin_center') ): the_row(); ?>["<?php the_sub_field('center_name' and <?php the_sub_field('longitude'); ?>],<?php endwhile ....
Thanks guys. I will stick to the push solution (is the one that the guys of ACF plugIn gave me to solve this problem. It works fine. Tried something with befzzs code but didn’t work. To show exactly what I needed see the original post. The array "markers" is the desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.