Retrieving nested, or multidimensional data, can easily be achieved with the Pro Plugin.
The Pro Plugin allows you to step down through multidimensional array data using array keys and indexes to get your data. This works in both the template function and also the shortcode.
Our latest update to the PRO plugin now allows you to grab multiple array keys so that you can easily pick and choose which items from an array you want to display.
Let’s pretend that your API returned the data below. You could easily get the nickname of the first item, or you could get all nicknames, or you could get both the nickname and id of the second item. The possibilities are endless as to which data you can retrieve.
Array(
[name] => Array(
[0] => Array(
[nickname] => Cowboy,
[id] => 24538
),
[1] => Array(
[nickname] => Yeehaa,
[id] => 49890
),
[2] => Array(
[nickname] => Horse,
[id] => 14377
)
)
)
The shortcode and template functions to retrieve these various types of data would look something like this:
Get the id of index 1
[wpgetapi_endpoint api_id="my_api" endpoint_id="my_endpoint" keys="name,1,id"]
wpgetapi_endpoint( "my_api", "my_endpoint", array(), array( "name", 1, "id" ) );
Get the nickname of index 2
[wpgetapi_endpoint api_id="my_api" endpoint_id="my_endpoint" keys="name,2,nickname"]
wpgetapi_endpoint( "my_api", "my_endpoint", array(), array( "name", 2, "nickname" ) );
Get the nicknames of all items
For simplicity, we’ll just show what to add into the keys attribute.
Each item is wrapped in curly braces and we use the pipe symbol as the key seperator.
keys="{name|0|nickname},{name|1|nickname},{name|2|nickname}"
wpgetapi_endpoint( "my_api", "my_endpoint", array(), array( "{name|0|nickname}", "{name|1|nickname}", "{name|2|nickname}" ) );
Get all items
keys="name"
wpgetapi_endpoint( "my_api", "my_endpoint", array(), array( "name" ) );
Get all nickname and id of index 0
keys="name,0"
wpgetapi_endpoint( "my_api", "my_endpoint", array(), array( "name", 0 ) );
This can be combined with the format=”html” attribute to output your required data as HTML.