Short version:
I need to make an array go from this:
$locations = array(
[0] => 'first location',
[1] => 'second location'
);
To this:
$locations = array( 'first location', 'second location' );
Long version
I’m using a plugin in WordPress called MapPress to generate a map dynamically. The I need to print an array of values into another array so it can generate a map. The documentation indicates that I can do this dynamically, but it really seems to mean that I can generate the map, as long as I know how many map points I need. I’d like to make the map generate based on however many of these custom posts are filled in.
I’m pulling custom information from the post to populate the fields and storing the collected array into another array. I’m using the method discussed here to pull each post’s info that I need and storing it in an array called $locations. Based on the documentation, I need to print the array into this array without the key value (“[0] =>”) but I can’t seem to find out how to do this efficiently. It seems like this was a problem that another person had, but was resolved for her specific need that doesn’t work with mine.
The code used to do all of this is below.
// Let's make a new map.
$mymap = new Mappress_Map(array("width" => 800));
// Run a loop to grab all posts of type "location"
global $post;
$tmp_post = $post;
$args = array( 'post_type' => 'location', 'posts_per_page'=> -1 );
$myposts = get_posts( $args );
$locations = array();
foreach( $myposts as $post ) : setup_postdata($post);
// Grab all the post's necessary data for creation of the map
$title = get_the_title();
$id = get_the_ID();
$location_address = get_field("location_address");
$location_excerpt = get_field("location_excerpt");
// Plug that data into Mappress' stuff, using dynamic variables
$mypoi = new Mappress_Poi(array(
"title" => $title,
"body" => $location_excerpt,
"address" => $location_address .'<a href="'.$id.'">More Information >></a>'
));
// This converts the address to a longitude/latitude location for the plugin's use
$mypoi->geocode();
// this is where I get hung up. I need to print the array without the key values
$locations[] = $mypoi;
endforeach;
$post = $tmp_post;
// print_r($locations); when I print_r them like they are, they have key values
$mymap->pois = array($locations); //this generates all the maps POIs to create the map
echo $mymap->display(array("directions"=>"none")); // this just displays the map
Long winded, I know. But it’s a specific problem that may be easier to solve with all the information available.
Thanks!
EDIT:
$locations was printing what I wanted it to, but @mario made me realize that it should accept the array as is. As I had it set up, it was as so:
$locations = array();
$mymap->pois = array($locations);
Which means it printed out:
$mymap->pois = array( array('location info 1', 'location info 2'));
All I needed to do was this;
$mymap->pois = $locations;
And it worked perfectly. I feel so dumb. I needed this though in order to finally get my answer. Thanks for your suggestions everyone!
This will print any array (in this case
$locations
) as you wish.$o
contains the output that you should print.